如何使用新的ASP.NET Identity 2.0角色和授权属性?

use*_*435 11 c# asp.net asp.net-identity

我正在使用新的ASP.NET Identity 2.0系统.我知道我可以检查用户是否处于这样的角色:

bool isAdmin = UserManager.IsInRole(User.Identity.GetUserId(), 
   "Customer Account Admin");
Run Code Online (Sandbox Code Playgroud)

我想这个代码可以在运行某些代码之前编写以检查,但是[Authorize]属性呢.我曾经说过:

[Authorize(Role="Customer Account Admin")]
Run Code Online (Sandbox Code Playgroud)

这不再起作用了,因为我不再使用旧的成员资格或角色管理.我怎么把两者放在一起?或者,我如何防范应用程序的某些部分无法供正确角色的成员使用?

编辑1:我不相信它有效.我在管理页面上放置了以下Authorize属性,我可以将代码作为"客户帐户用户"执行

   [Authorize(Roles = "Customer Service Admin, Savitas Admin")]
    public partial class _default : System.Web.UI.Page
Run Code Online (Sandbox Code Playgroud)

此外,我想阻止该页面被未经授权的用户看到.我们有阻止菜单的代码,但我仍然可以输入管理页面的URL,未经授权的用户可以看到它

 if (HttpContext.Current.User.IsInRole("Customer Account Admin"))
                    //
                    {
                    }
                    else
                    {
                        mi = radmenu1.Items.FindItemByText("Admin");
                        radmenu1.Items.Remove(mi);
                    }
Run Code Online (Sandbox Code Playgroud)

EDIT2:我们在ASpNetRoles表中手动创建了角色,并将用户映射到ASPNetUsersToRoles表中的角色.用户与"客户服务管理员"等角色之间存在映射.我们将用户添加到具有以下内容的角色,但我认为它不起作用:

if (manager.AddToRole(manager.FindByName(UserName.Text).Id, "Customer Account Admin").Succeeded)
                                {
                                    c.logActivity("Register.aspx.cs", "REG_USER_ROLE", "Setting user to Admin role succeeded");
                                }
Run Code Online (Sandbox Code Playgroud)

当普通用户登录时,他们不会通过在地址栏中输入管理员页面获得管理员菜单:

http://localhost:53620/Admin/default
Run Code Online (Sandbox Code Playgroud)

我怎么阻止它?

编辑3:我试图按照你的示例Eric阻止所有用户访问管理员页面,但我再次以客户用户身份登录,仍然在地址栏中输入上述内容并进入页面.这有什么问题:

    <configuration>
  <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <connectionStrings>
    ...
  </connectionStrings>
  <location path="~/Admin/default.aspx">
    <system.web>
      <authorization>
        <allow roles="Customer Service Admin" />
        <deny users="*"/>
      </authorization>
Run Code Online (Sandbox Code Playgroud)

Edit4:切换到path ="Admin/default.aspx"会出现以下配置文件错误:

Configuration Error 
  Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

 Parser Error Message: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.  This error can be caused by a virtual directory not being configured as an application in IIS.

Source Error: 



Line 66:         </controls>
Line 67:       </pages>
Line 68:       <membership>
Line 69:         <providers>
Line 70:           <!--        ASP.NET Membership is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
Run Code Online (Sandbox Code Playgroud)

Eri*_*sch 7

我已经进行了多次测试,但我无法重现您的问题.我使用过带和没有空格的角色,以及多个角色.一切都按预期工作.

你是如何添加角色的?这就是我在做的方式.

var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
roleManager.Create(new IdentityRole("This Is A Test"));
UserManager.AddToRole(user.Id, "This Is A Test");
Run Code Online (Sandbox Code Playgroud)

更新:

ASP.NET有三个主要组件:WebForms,MVC和Web Pages.您正在使用WebForms(不是经典的asp.net或任何其他术语).

有几种方法可以按角色保护页面,但最简单的方法是使用location元素在web.config中执行此操作.再一次,这与ASP.NET身份或旧式角色或其他任何事实无关 ......它都是基于通用的IPrincipal和IIdentity接口,它们是基础asp.net的一部分.例如,以下内容允许所有管理员访问该站点并拒绝所有其他用户,但允许MyUsers角色中的用户访问CoolStuff.aspx:

<configuration>    
 <system.web>    
      <authorization>    
           <allow roles="Administrators" />    
           <deny users="*"/>    
      </authorization>    

 </system.web>

 <!-- Allow all "MyUsers" role users to access CoolStuff.aspx -->    
 <location path="CoolStuff.aspx">    
      <system.web>    
           <authorization>    
                <allow roles="MyUsers" />    
           </authorization>    
      </system.web>    
 </location>    
</configuration>
Run Code Online (Sandbox Code Playgroud)

但请注意,如果您正在使用路由,则可能会将同一页面路由到两个不同的URL,这意味着可以从一个URL访问它,但如果您不小心您的权限则不能访问另一个URL.