【问题标题】:Asp.net membership logout doesn't workAsp.net 会员注销不起作用
【发布时间】:2014-02-13 12:34:39
【问题描述】:

我正在尝试在我的应用程序中实现成员资格,但我遇到了问题。 这是我在登录时创建票证的登录代码:

Kullanici kullanici = KullaniciProvider.KulaniciGetirEmailSifreIle(LoginControl.UserName, LoginControl.Password);
if (kullanici != null)
{
    Session["Kullanici"] = kullanici;
    string rol = "Firma";
    if (kullanici.KullaniciTipi == "Admin")
        rol = "Admin";


    FormsAuthentication.SetAuthCookie(LoginControl.UserName, true);
    FormsAuthenticationTicket ticket = new
    FormsAuthenticationTicket(
         1,
         LoginControl.UserName,
         System.DateTime.Now,
         System.DateTime.Now.AddMinutes(20),
         true,
         rol,
         FormsAuthentication.FormsCookiePath);
    string encTicket = FormsAuthentication.Encrypt(ticket);
    Response.Cookies.Add(new
    HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
    Response.Redirect(FormsAuthentication.GetRedirectUrl(LoginControl.UserName, true));
}

这是我获取角色的 Global.asax 文件:

if (HttpContext.Current.User != null)
{
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
        if (HttpContext.Current.User.Identity is FormsIdentity)
        {
            FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
            FormsAuthenticationTicket ticket = (id.Ticket);
            if (!FormsAuthentication.CookiesSupported)
            {
                //If cookie is not supported for forms authentication, then the
                //authentication ticket is stored in the URL, which is encrypted.
                //So, decrypt it
                ticket = FormsAuthentication.Decrypt(id.Ticket.Name);
            }
            // Get the stored user-data, in this case, user roles
            if (!string.IsNullOrEmpty(ticket.UserData))
            {
                string userData = ticket.UserData;
                string[] roles = userData.Split(',');
                //Roles were put in the UserData property in the authentication ticket
                //while creating it
                HttpContext.Current.User =
                  new System.Security.Principal.GenericPrincipal(id, roles);
            }
        }
    }
}

这是我的 Site.Master。当用户注销时,代码不起作用。用户无法正常退出

protected void LoginStatusGiris_LoggingOut(object sender, LoginCancelEventArgs e)
{

    Session.Clear();
    Session.Abandon();
    Session.RemoveAll();
    deleteCookies();
    FormsAuthentication.SignOut();
    FormsAuthentication.RedirectToLoginPage();


}
private void deleteCookies()
{
    string[] cookies = Request.Cookies.AllKeys;
    foreach (string cookie in cookies)
    {
        Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
    }
}

我已删除所有 cookie 但找不到解决方案。

【问题讨论】:

    标签: c# asp.net loginview


    【解决方案1】:
    protected void LoginStatusGiris_LoggingOut(object sender, EventArgs e)
        {
    
            Session.Clear();
            Session.Abandon();
            Session.RemoveAll();
            deleteCookies();
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
    
    
        }
    

    使用 EventArgs 而不是 LoginCancelEventArgs

    【讨论】:

    • 你能举出更具体的例子吗?
    • 让我这样解释:您的 LoginStatusGiris_LoggingOut 方法正在使用 LoginCancelEventArgs。只需使用 EventArgs 更改 LoginStatusGiris_LoggingOut。就像我在上面输入的答案一样。
    • 不,我的朋友我已经做到了,但仍然没有工作。
    【解决方案2】:

    您应该改用LoginStatusGiris LoggedOut 事件,并像这样清除会话和cookie:

    protected void LoginStatusGiris_LoggedOut(Object sender, System.EventArgs e)
    {
        FormsAuthentication.SignOut();
        Session.Abandon();
        deleteCookies();
        FormsAuthentication.RedirectToLoginPage();
    }
    
    private void deleteCookies()
    {
        string[] cookies = Request.Cookies.AllKeys;
        foreach (string cookie in cookies)
        {
            Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);
        }
    }
    

    【讨论】:

    • 不,不幸的是,这不起作用。我正在尝试从默认页面用户登录重定向到登录页面,然后页面重定向回默认页面。这是有效的,但是当用户注销时,我杀死了所有会话 cookie,但登录视图仍然留在登录的模板中。跨度>
    • 您是否正在使用 loginView 进行自定义操作?可以分享一下所有涉及到 LoginView 的代码吗?
    猜你喜欢
    • 2011-05-17
    • 2013-02-13
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多