0

アプリケーションにメンバーシップを実装しようとしていますが、問題があります。これは、ログイン時にチケットを作成しているログイン コードです。

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.Whenユーザーのログアウトコードです。ユーザーが正しくサインアウトできない

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 を削除しましたが、解決策が見つかりませんでした。

4

2 に答える 2

0

代わりにイベントを使用し、次の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);
    }
}
于 2014-02-13T12:58:49.703 に答える