使用自定义 table 在 MVC 和 Web API 中实现基于角色的授权
Implementing role based authorization in MVC and Web API with custom table
我继承了一个带数据库的应用程序。数据库有以下 table 与身份验证和授权相关。
用户Table
用户名
密码
UserTypeId
用户类型Table
UserTypeId
UserTypeDesc
用户类型 table 存储用户的角色,例如管理员、编辑等
如果我想像下面这样实现授权
[Authorize(Roles="Admin, Editor")]
public IHttpActionResult GetOrders()
{
//Code here
}
我应该在哪里以及编写什么代码,以便授权属性可以使用这些角色?
编辑
我已经有一个数据库。所以我不能使用 AspNetUserRoles 或 AspNetRoles tables。我需要使用我的自定义设置角色 tables.
编辑2
正如@Nkosi 所问,这里是如何实现身份验证的代码片段。实际实现调用业务层服务并执行加密和其他操作,但我简化了代码片段
public HttpResponseMessage Authenticate(User user)
{
var isValid = myRepository.Exists(a => a.UserName == user.UserName && a.Password == user.Password);
if(isValid)
{
FormsAuthentication.SetAuthCookie(user.UserName,false);
}
}
此方法从用户输入用户名和密码的登录页面调用
你的问题很简单。您只需将这两个表分别与 AspNetUserRoles 和 AspNetRoles 表同步。实际上,Authorize 属性默认检查这两个表。所以你的角色需要反映在他们身上。如果您 select MVC 模板项目,这些表默认由 EF 创建。
使用这些答案作为参考
Having Trouble with Forms Authentication Roles
FormsAuthentication Roles without Membership
像最初那样在登录时设置身份验证 cookie 后,
您在 Global.asax.cs
中执行以下操作
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
FormsIdentity formsIdentity = new FormsIdentity(ticket);
ClaimsIdentity claimsIdentity = new ClaimsIdentity(formsIdentity);
//get the user from your custom tables/repository
var user = myUserRepository.GetUserByEmail(ticket.Name);
if(user!=null){
var userTypeId = user.UserTypeId;
var role = myUserTypeRepository.GetUserTypeById(userTypeId);
if(role != null) {
//Assuming the roles for the user e.g. Admin, Editor, etc.
// is in the UserTypeDesc property
claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role.UserTypeDesc));
}
}
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
System.Threading.Thread.CurrentPrincipal = claimsPrincipal ;
if (System.Web.HttpContext.Current != null) {
System.Web.HttpContext.Current.User = claimsPrincipal ;
}
}
}
他们如何实现它的好处是它使用 ClaimsIdentity
和 ClaimsPrincipal
对象处理基于声明的角色,而无需将角色放入用户的 cookie 中。它还在 Global.asax.cs
文件中处理身份验证,而无需诉诸自定义授权属性。
我继承了一个带数据库的应用程序。数据库有以下 table 与身份验证和授权相关。
用户Table
用户名
密码
UserTypeId
用户类型Table
UserTypeId
UserTypeDesc
用户类型 table 存储用户的角色,例如管理员、编辑等
如果我想像下面这样实现授权
[Authorize(Roles="Admin, Editor")]
public IHttpActionResult GetOrders()
{
//Code here
}
我应该在哪里以及编写什么代码,以便授权属性可以使用这些角色?
编辑
我已经有一个数据库。所以我不能使用 AspNetUserRoles 或 AspNetRoles tables。我需要使用我的自定义设置角色 tables.
编辑2
正如@Nkosi 所问,这里是如何实现身份验证的代码片段。实际实现调用业务层服务并执行加密和其他操作,但我简化了代码片段
public HttpResponseMessage Authenticate(User user)
{
var isValid = myRepository.Exists(a => a.UserName == user.UserName && a.Password == user.Password);
if(isValid)
{
FormsAuthentication.SetAuthCookie(user.UserName,false);
}
}
此方法从用户输入用户名和密码的登录页面调用
你的问题很简单。您只需将这两个表分别与 AspNetUserRoles 和 AspNetRoles 表同步。实际上,Authorize 属性默认检查这两个表。所以你的角色需要反映在他们身上。如果您 select MVC 模板项目,这些表默认由 EF 创建。
使用这些答案作为参考
Having Trouble with Forms Authentication Roles
FormsAuthentication Roles without Membership
像最初那样在登录时设置身份验证 cookie 后,
您在 Global.asax.cs
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
FormsIdentity formsIdentity = new FormsIdentity(ticket);
ClaimsIdentity claimsIdentity = new ClaimsIdentity(formsIdentity);
//get the user from your custom tables/repository
var user = myUserRepository.GetUserByEmail(ticket.Name);
if(user!=null){
var userTypeId = user.UserTypeId;
var role = myUserTypeRepository.GetUserTypeById(userTypeId);
if(role != null) {
//Assuming the roles for the user e.g. Admin, Editor, etc.
// is in the UserTypeDesc property
claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role.UserTypeDesc));
}
}
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
System.Threading.Thread.CurrentPrincipal = claimsPrincipal ;
if (System.Web.HttpContext.Current != null) {
System.Web.HttpContext.Current.User = claimsPrincipal ;
}
}
}
他们如何实现它的好处是它使用 ClaimsIdentity
和 ClaimsPrincipal
对象处理基于声明的角色,而无需将角色放入用户的 cookie 中。它还在 Global.asax.cs
文件中处理身份验证,而无需诉诸自定义授权属性。