简体   繁体   English

需要帮助以Asp.Net身份1运行[Authorize(Roles =“ Admin”)]和User.isInRole(“ Admin”)

[英]Need help getting [Authorize(Roles=“Admin”)] and User.isInRole(“Admin”) working in Asp.Net identity 1

I just don't get how these two potentially incredibly useful functions are supposed to work. 我只是不知道这两个潜在有用的功能应该如何工作。

[Authorize(Roles="Admin")] //Method A

and

User.isInRole("Admin") //Method B

They are clearly not working right now for me. 他们现在显然不能为我工作。 I did a few hours of research: 我做了几个小时的研究:

I read that you need to implement System.Web.Security.RoleProvider, then set it up in the web config: 我读到您需要实现System.Web.Security.RoleProvider,然后在Web配置中进行设置:

 <roleManager defaultProvider="OdbcRoleProvider" 
  enabled="true"
  cacheRolesInCookie="true"
  cookieName=".ASPROLES"
  cookieTimeout="30"
  cookiePath="/"
  cookieRequireSSL="false"
  cookieSlidingExpiration="true"
  cookieProtection="All" >
  <providers>
    <clear />
    <add
      name="OdbcRoleProvider"
      type="Samples.AspNet.Roles.OdbcRoleProvider"
      connectionStringName="OdbcServices" 
      applicationName="SampleApplication" 
      writeExceptionsToEventLog="false" />
  </providers>
</roleManager>

This caused the RoleProvider I implemented to by constructed, but the role checking functions were certainly not calling any of the methods in there. 这造成了我实现的RoleProvider的构造,但是角色检查功能肯定没有在其中调用任何方法。

I then read that Asp.NET Identity does away with the RoleProvider , now you need to do this: 然后,我读到Asp.NET Identity取消了RoleProvider ,现在您需要这样做:

 <modules runAllManagedModulesForAllRequests="true">
  <remove name="FormsAuthenticationModule" />
  <remove name="RoleManager" />
</modules>

And I did that. 而我做到了。

I have a custom UserManager that connects to my postgres backend. 我有一个自定义的UserManager连接到我的postgres后端。 The problem is that whenever I use it, I need to instantiate one. 问题是,每当我使用它时,我都需要实例化一个。 It seems to me that if Functions A and B are going to work, then the UserManager I have implemented needs to be referenced in some sort of config file so Asp.NET knows about it implicitly. 在我看来,如果要使用功能A和B, 则需要在某种配置文件中引用我实现的UserManager,以便Asp.NET隐式知道它。 This would be just like the RoleManager in the past. 就像过去的RoleManager一样。

How does ASP.NET identity alter how Functions A and B check the roles from the old RoleProvider using behavior? ASP.NET身份如何改变功能A和B如何使用行为从旧的RoleProvider检查角色?

I figured it out. 我想到了。

When you call the login code like this: 当您像这样调用登录代码时:

var user = await UserManager.FindAsync(model.Email, model.Password);
            if (user != null && user.PasswordRequiresReset == false)
            {
                await SignInAsync(user, model.RememberMe); //triggers IUserRoleStore.GetRolesAsync
                return RedirectToLocal(returnUrl);
            }

SignInAsync triggers GetRolesAsync from the IUserRoleStore: SignInAsync从IUserRoleStore触发GetRolesAsync:

public Task<IList<string>> GetRolesAsync(TUser user) //where TUser is an ApplicationUser
    {

        Func<object, IList<string>> getRoles = (object user2) =>
        {
            TUser user3 = (TUser)user2;
            return user3.Roles.Select(x => x.Role.Name).ToList();
        };


        return Task.Factory.StartNew(getRoles, user);

    }

In the above, I chose to simply generate the roles from the roles I already loaded from the db and stored in the ApplicationUser object when I created it in FindAsync. 在上面的内容中,我选择仅根据在FindAsync中创建时已从数据库加载并存储在ApplicationUser对象中的角色来生成角色。

The roles from GetRolesAsync must be loaded in the cookie somewhere where they can be accessed quickly and easily by Functions A and B. 必须将GetRolesAsync中的角色加载到cookie中的某个位置,以便可以通过函数A和B轻松快速地对其进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM