Arm*_*hul 1 c# asp.net asp.net-mvc entity-framework asp.net-mvc-5
我有一个应用程序,我使用Windows身份验证.没有太详细,我的应用程序设置了一系列用户,然后给予管理员权限来创建缺席.如果他们没有管理员权限,那么他们就无法更改用户或创建缺席.
我想根据数据库中的admin标志是否设置为true来限制对某些控制器/操作的访问.我工作的用户属于多个组,没有管理员组,我可以将其包含在Authorize属性角色字符串中.
我在这里遵循了教程,但由于我有一个数据库第一实体框架模型,实体类继承自DbContext而不是来自身份上下文.
当我运行应用程序时,我的代码引发了一个错误说:"mscorlib.dll中发生了'System.InvalidOperationException'类型的异常但未在用户代码中处理
附加信息:实体类型IdentityRole不是当前上下文模型的一部分."我点击查看详细信息,我看到这个"实体类型IdentityRole不是当前上下文模型的一部分."
这是发生错误的代码片段:
AbsencesEntities context = new AbsencesEntities();
AbsenceRepository absenceRepository = new AbsenceRepository(context);
IdentityResult IdRoleResult;
IdentityResult IdUserResult;
// Create a RoleStore object by using the UserSecurity object.
// The RoleStore is only allowed to contain IdentityRole objects.
var roleStore = new RoleStore<IdentityRole>(context);
// Create a RoleManager object that is only allowed to contain IdentityRole objects
// When creating the RoleManager object, you pass in (as a parameter) a new RoleStore
var roleMgr = new RoleManager<IdentityRole>(roleStore);
// Then, you create the "canEdit" role if it doesn't already exist
if(!roleMgr.RoleExists("canEdit"))
{
IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
}
Run Code Online (Sandbox Code Playgroud)
只是为了澄清我没有在配置文件中指定任何其他上下文.
我必须有办法使用Windows身份验证并使用返回的LAN ID来检查它是否存在于数据库中.然后使用它来检查数据库中的Admin标志是否为true.
您可以从authorizeattribute继承并覆盖authorizecore,然后使用该属性装饰您的控制器和/或方法以处理此方案.例如:
public class PageAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (/*Rolemanager check*/) {
return true;
}
return false;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{"action", "PageDenied"}
,
{"controller", "Authorization"}
});
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用它httpContext.User.Identity.Name来检查用户名.
[PageAuthorize]
public class PageController : Controller
{}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
860 次 |
| 最近记录: |