我为我的用户创建了自定义身份验证和授权 . 我面临的问题是如何让mvc检查我的users表中的角色是否与我的控制器上的[Authorize(Role)]匹配,以便将httpauthorised设置为true . 以下是我的习惯课程 .
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Controller.TempData["ErrorDetails"] = "You must be logged in to access this page";
filterContext.Result = new RedirectResult("~/User/Login");
return;
}
if (filterContext.HttpContext.Request.IsAuthenticated)
{
using (var db = new GManagerDBEntities())
{
var authorizedRoles = (from u in db.Users
where u.Username == filterContext.HttpContext.User.Identity.Name
select u.Role).FirstOrDefault();
Roles = String.IsNullOrEmpty(Roles) ? authorizedRoles.ToString() : Roles;
}
}
if (filterContext.Result is HttpUnauthorizedResult)
{
filterContext.Controller.TempData["ErrorDetails"] = "You do nat have necessary rights to access this page";
filterContext.Result = new RedirectResult("~/User/Login");
return;
}
}
public CustomAuthorizeAttribute(params object[] roles)
{
if (roles.Any(r => r.GetType().BaseType != typeof(Enum)))
throw new ArgumentException("roles");
this.Roles = string.Join(",", roles.Select(r => Enum.GetName(r.GetType(), r)));
}
}
下面是我的装饰控制器
[CustomAuthorize(Role.Administrator)]
[HttpGet]
public ActionResult CreateEmployees()
{
return View();
}
和我的角色
public enum Role
{
Administrator = 1,
UserWithPrivileges = 2,
User = 3,
}
和模型
public class UserModel
{
public int UserID { get; set; }
[Required]
[Display(Name="Username:")]
public string Username { get; set; }
[Required]
public string Password { get; set; }
public int Role { get; set; }
}
看到意见清晰pastie
我试图解决这个问题的链接,但我似乎无法把它拼凑起来MVC 3 Authorize custom roles http://forums.asp.net/p/1573254/3948388.aspx
2 回答
使用@VikasRana共享的链接http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF
我摆脱了我的枚举角色和我的方法
然后,我将模型中的角色更改为字符串,例如User.Role =“Admin”而不是int . 在我的onAuthorization方法中,我将其更改为:
并在我的global.asax中添加了这个 .
上面的方法并不理想 . 它可以为每个简单的页面请求运行大约3次或更多次 .
So here is solution 2:better solution 实现自定义角色提供程序,因为我们已经在使用自定义角色实现 . 只需按照此链接http://techbrij.com/custom-roleprovider-authorization-asp-net-mvc
感谢Gotalove在Global.asax中使用此方法 . 对于任何尝试使用实体框架进行自定义表单身份验证(FormsAuthentication,FormsAuthenticationTicket)的人来说,这里有一些帮助 .
登录控制器SetAuthTicket
WebConfig
Global.asax(来自上面的例子)