【发布时间】:2014-06-08 15:45:05
【问题描述】:
我正在使用新的 ASP.NET Identity 2.0 系统。我知道我可以检查用户是否担任这样的角色:
bool isAdmin = UserManager.IsInRole(User.Identity.GetUserId(),
"Customer Account Admin");
我想这段代码可以写成在某些代码运行之前进行检查,但是 [Authorize] 属性呢?我曾经可以说:
[Authorize(Role="Customer Account Admin")]
这不再起作用,因为我不再使用旧的成员资格或角色管理。我怎样才能把两者放在一起?或者我如何防止应用程序的某些部分对正确角色的成员不可用?
Edit1:我不相信它有效。我将以下 Authorize 属性放在管理页面上,我能够以“客户帐户用户”的身份执行代码
[Authorize(Roles = "Customer Service Admin, Savitas Admin")]
public partial class _default : System.Web.UI.Page
另外,我想阻止未经授权的用户看到该页面。我们有代码来阻止菜单,但我仍然可以输入管理页面的 URL,并且未经授权的用户可以看到它
if (HttpContext.Current.User.IsInRole("Customer Account Admin"))
//
{
}
else
{
mi = radmenu1.Items.FindItemByText("Admin");
radmenu1.Items.Remove(mi);
}
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");
}
当普通用户登录时,他们不会通过在地址栏中输入进入管理页面的管理菜单:
http://localhost:53620/Admin/default
我该如何阻止它?
Edit3:我尝试按照您的示例 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>
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
【问题讨论】:
-
哎呀,这真是不升级的理由。如果不再有内置方法,我假设您可以编写自定义属性。
-
您的假设是错误的。 Authorize 属性适用于 ASP.NET 标识。原因是 Authorize 属性不适用于 Membership 或 Identity,它适用于 IPrincipal 和 IIdentity,两者都与系统无关。您的问题很可能是因为您的姓名中有空格或未启用角色。
-
该错误告诉您问题“此错误可能是由于虚拟目录未在 IIS 中配置为应用程序造成的”。在我看来,您在虚拟目录中有一个未配置为应用程序的 web.config。
-
我的项目的 Account 文件夹中有一个 web.config,但我删除了它。我无法摆脱这个问题。我在这里遵循了 SO 帖子中的所有建议,但无济于事。我在 IIS Express 本地运行。
-
我解决了这个问题。它与 SO 上的其他人不同。我在配置文件中有两个
区域。
标签: c# asp.net asp.net-identity