问题创造持久的身份验证Cookie:ASP.NET MVC [英] Problem creating persistent authentication cookie: ASP.NET MVC

查看:137
本文介绍了问题创造持久的身份验证Cookie:ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是我的code创建一个身份验证Cookie:

OK, here's my code to create an authentication cookie:

        // get user's role
        List<UserType> roles = rc.rolesRepository.GetUserRoles(rc.userLoginRepository.GetUserID(userName));
        List<string> rolesList = (from r in roles
                                 select r.ToString()).ToList();
        string[] rolesArr = rolesList.ToArray();

        // create encryption cookie
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                1,
                userName,
                DateTime.Now,
                DateTime.Now.AddDays(90),
                createPersistentCookie,
                String.Join(";",rolesArr) //user's roles 
                );

        // add cookie to response stream
        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

        System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
        //FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);

这是我的code在Global.asax中设置用户角色到用户身份:

And here's my code in Global.asax to set the user roles into the user identity:

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie == null || authCookie.Value == "")
        {
            return;
        }
        FormsAuthenticationTicket authTicket = null;
        try
        {
            authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            string[] roles = authTicket.UserData.Split(new char[] { ';' });
            if (Context.User != null)
            {
                Context.User = new System.Security.Principal.GenericPrincipal(Context.User.Identity, roles);
            }
        }
        catch
        {
            return;
        }
    }

不过,如果createPersistentCookie在上面的例子是TRUE,则不会创建永久性的Cookie。如果我去掉最后一行,像这样:

However, if "createPersistentCookie" is TRUE in the top example, no persistent cookie is created. If I uncomment the last line like so:

        //System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
        FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);

那么持久性cookie是我的硬盘驱动器上创建。但随后在Global.asax code,在authTicket中的UserData字段为空,所以无法建立正确的角色!

then the persistent cookie is created on my hard drive. BUT then in the Global.asax code, the UserData field in "authTicket" is blank, so I can't set up the roles properly!

所以,我必须用SetAuthCookie创建一个永久性的Cookie,但后来由于某些原因的UserData字段从持久性cookie消失。

So I have to use SetAuthCookie to create a persistent cookie, but then for some reason the UserData field disappears from the persistent cookie.

什么是这个问题的答案?

What is the answer to this??

推荐答案

要创建你需要设置的过期属性:

To create a persistent cookie you need to set the Expires property:

if (authTicket.IsPersistent)
{
    authCookie.Expires = authTicket.Expiration;
}

这篇关于问题创造持久的身份验证Cookie:ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆