栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

设置HttpContext.Current.User

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设置HttpContext.Current.User

这是我最终使用的版本,该版本基于@ AdamTuliper-
MSFT的回答。它仅应在登录后但在重定向之前使用,以允许其他代码访问

HttpContext.User

  • 如果已通过身份验证,请勿执行任何操作
  • 不修改cookie,因为它仅在此请求的生存期内使用
  • 缩短一些内容,并使用userdata使其更加安全(永远不能为null,但是…)

在调用SetAuthcookie()之后调用此函数,如下所示:

// in login functionFormsAuthentication.SetAuthcookie(model.UserName, model.RememberMe);AuthenticateThisRequest();private void AuthenticateThisRequest(){    //NOTE:  if the user is already logged in (e.g. under a different user account)    //       then this will NOT reset the identity information.  Be aware of this if    //       you allow already-logged in users to "re-login" as different accounts     //       without first logging out.    if (HttpContext.User.Identity.IsAuthenticated) return;    var name = FormsAuthentication.FormscookieName;    var cookie = Response.cookies[name];     if (cookie != null)    {var ticket = FormsAuthentication.Decrypt(cookie.Value);        if (ticket != null && !ticket.Expired)        { string[] roles = (ticket.UserData as string ?? "").Split(','); HttpContext.User = new GenericPrincipal(new FormsIdentity(ticket), roles);        }    }}

编辑: 删除对Request.cookies的调用,如@ AdamTuplier-MSFT所述。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427406.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号