簡體   English   中英

Asp.net成員資格注銷不起作用

[英]Asp.net membership logout doesn't work

我正在嘗試在我的應用程序中實現成員身份,但是我遇到了問題。 這是我的登錄代碼,我正在登錄時創建票證:

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,但找不到解決方案。

protected void LoginStatusGiris_LoggingOut(object sender, EventArgs e)
    {

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


    }

使用EventArgs代替LoginCancelEventArgs

您應該改用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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM