Cyb*_*cop 1 asp.net-mvc authorization roleprovider asp.net-mvc-4
我想建立一个注册系统,在添加用户的同时,你可以选择你可以给他/她的角色类型.并且根据他/她的角色,将决定是否可以访问控制器中的某些动作.
例如,假设有两个角色,即管理员和开发人员.如下所述,只允许具有管理员角色的用户访问以下操作.
[Authorize(Roles = "admin"]
public ActionResult CreateUser()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,我必须实施我的习惯RoleProvider或IPrincipal?我试图找到一些例子,但没有得到我正在寻找的东西.以下是我的RegisterModel目前的样子
public class RegisterModel
{
[Key]
public Guid Id;
[Required]
[Display(Name="First Name")]
public string FirstName {get; set;}
[Required]
[Display(Name="Last Name")]
public string LastName {get; set;}
[Required]
[Display(Name="Email Id")]
[DataType(DataType.EmailAddress)]
public string EmailId {get; set;}
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[Display(Name = "Confirm Password")]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Role")]
public UserRole Role { get; set; }
}
public class UserRole
{
[Key]
public int RoleId { get; set; }
public string RoleName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望在添加用户并使用Custom Authorize属性时决定角色.任何人都知道可以解决我的问题的文章或博客?或者任何建议,该怎么办?
最近我实现了角色授权而不使用成员提供商.认为这可能会对你有所帮助.我有一个包含UserName,Password和Role的数据库表,我需要检查数据库的角色.
下面是我自定义的RoleFilter类.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplicationrazor.Models.ActionFilters
{
public class RoleFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (GetCurrentUserRole() != "Admin")// Check the Role Against the database Value
{
filterContext.Result = new RedirectResult("~/Redirect/NoPermission");
return;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
[RoleFilter]//Check the Role, if not allowed redirect to NoPermission view
public ActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5818 次 |
| 最近记录: |