首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpContext.Current.User != HttpContext.User?

HttpContext.Current.User != HttpContext.User?
EN

Stack Overflow用户
提问于 2013-05-22 13:26:20
回答 1查看 11.6K关注 0票数 8

全局asax中的HttpContext.Current.User与动作方法中的HttpContext.User不一样吗?我给用户分配了一些角色,但他们似乎迷路了。

下面的代码显示了正在发生的事情。这两个断言在用户登录时都会被击中,首先是在全局asax中,然后是action方法。然而,他们给出了不同的结果。

首先,这是:

代码语言:javascript
复制
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    // ... omitted some code to check user is authenticated
    FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;

    string[] roles = new string[] { "admin", "user" };

    HttpContext.Current.User =
        new System.Security.Principal.GenericPrincipal(identity, roles);

    Assert(HttpContext.User.IsInRole("admin"));
}

在我的行动方法中:

代码语言:javascript
复制
public ActionResult Index()
{
    bool isAdmin = HttpContext.User.IsInRole("admin");

    Assert(isAdmin); // this fails, isAdmin is false

    // ...
}

我使用了以下资源

这就是答案

http://csharpdotnetfreak.blogspot.com/2009/02/formsauthentication-ticket-roles-aspnet.html

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-22 13:58:28

您的问题标签上写着“aspnet (3和4)",那么您可以选择使用以下选项来使您的生活更轻松吗?如果您使用的是MVC4InternetApplication模板中的简单成员资格,这将为您提供现成的服务):

  • WebSecurity.CreateUserAndAccount(name, password) -创建一个用户
  • Roles.AddUserToRole (和AddUserToRoles) -将用户添加到角色
  • Roles.IsUserInRole -测试用户是否在角色中
  • [Authorize(Roles = "admin")] - [Authorize]可以在整个控制器或操作上强制执行角色。

CreateUserAndAccount的优点是也很容易为UserProfile设置属性,例如:

代码语言:javascript
复制
WebSecurity.CreateUserAndAccount(newUser.UserName, newUser.Password,
    new { FullName = newUser.FullName, Email = newUser.Email, Timezone = newUser.TZ });
Roles.AddUserToRoles(newUser.UserName, new[] {"admin", "user"});

编辑,我知道上面没有回答你最初关于.User属性等价性的问题。

控制器中的HttpContext是一个属性:Controller.HttpContextHttpContext in global.asax.cs是静态类,这就是使用HttpContext.Current的原因。他们指的是同样的东西。

如果运行以下代码,您可以看到它们显然是“相同的主体”。所以问题是你分配的角色发生了什么?

代码语言:javascript
复制
protected void Application_AuthenticateRequest(object sender, EventArgs e) {
    ...
    FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
    string[] roles = new string[] { "admin", "user" };
    identity.Label = "test label";
    System.Security.Principal.GenericPrincipal ppl = new System.Security.Principal.GenericPrincipal(identity, roles);            
    HttpContext.Current.User = ppl;
... }

public ActionResult Index() {
    bool isAdmin = HttpContext.User.IsInRole("admin");
    bool isAdmin2 = System.Web.HttpContext.Current.User.IsInRole("admin");
    System.Web.Security.FormsIdentity identity = (System.Web.Security.FormsIdentity)HttpContext.User.Identity;

    // The label is carried through from Application_AuthenticateRequest to Index.
    string label = identity.Label;
}

问题是,您将一个GenericPrincipal分配给.User。根据RoleProvider的不同,可以在PostAuthenticateRequest期间覆盖(例如,由RoleManagerModule),然后(例如)转换为RolePrincipal。然后,这可以推迟到数据库(同样取决于提供者)来获得角色,所以重写您的角色。如果你用Application_OnPostAuthenticateRequest做这项工作,你可能会没事的。

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

https://stackoverflow.com/questions/16693043

复制
相关文章

相似问题

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