首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Asp.Net MVC FormsAuthenticationTicket

Asp.Net MVC FormsAuthenticationTicket
EN

Stack Overflow用户
提问于 2010-07-16 07:08:05
回答 1查看 3.5K关注 0票数 5

我在Logon方法中设置FormsAuthenticationTicket以手动创建身份验证cookie。如何验证身份验证cookie并为其分配Current.User对象。是在Global.asax页面中完成的吗?

登录代码:

代码语言:javascript
复制
    FormsAuthenticationTicket Authticket = new
                            FormsAuthenticationTicket(1,
                            model.UserName,
                            DateTime.Now,
                            DateTime.Now.AddYears(1),
                            true,
                            "",
                            FormsAuthentication.FormsCookiePath);

                string hash = FormsAuthentication.Encrypt(Authticket);

                HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

                if (Authticket.IsPersistent) Authcookie.Expires = Authticket.Expiration;

                Response.Cookies.Add(Authcookie);


                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }

                return RedirectToAction("Index", "Home");

如何读取此cookie并验证用户?到目前为止,我在global.asax文件中的代码:

代码语言:javascript
复制
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            FormsIdentity id = new FormsIdentity(authTicket);
            GenericPrincipal principal = new GenericPrincipal(id,null);
            Context.User = principal;
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-16 07:29:21

我将这种类型的代码移到了一个基本控制器中。在控制器类中有一个名为"OnAuthorization“的方法,它可以被覆盖。

已经有一段时间了,但我相信所有的请求(图片,css...等),在Global.asax中使用OnAuthorization方法。通过将授权向下推送到控制器,您只能获得对控制器/操作的请求

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3260766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档