这是自定义的授权从数据库检查权限。 例如,你有3个布尔变量的权限的帐户,客户端,配置 你想限制继的ActionResult
线,您可以添加,甚至允许在一个动作两个基于他们比地方的用户,比如你有一个方法这可以通过帐户和客户端权限进行访问比您可以添加以下行
[PermissionBasedAuthorize("Client, Account")]
下面这个方法是从数据库查询的bool。
public class PermissionBasedAuthorize : AuthorizeAttribute
{
private List<string> screen { get; set; }
public PermissionBasedAuthorize(string ScreenNames)
{
if (!string.IsNullOrEmpty(ScreenNames))
screen = ScreenNames.Split(',').ToList();
}
public override void OnAuthorization(HttpActionContext actionContext)
{
base.OnAuthorization(actionContext);
var UserId = HttpContext.Current.User.Identity.GetUserId();
ApplicationContext db = new ApplicationContext();
var Permissions = db.Permissions.Find(UserId);
if (screen == null || screen.Count() == 0)
{
actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
}
bool IsAllowed = false;
foreach (var item in screen)
foreach (var property in Permissions.GetType().GetProperties())
{
if (property.Name.ToLower().Equals(item.ToLower()))
{
bool Value = (bool)property.GetValue(Permissions, null);
if (Value)
{
IsAllowed = true;
}
break;
}
}
if (!IsAllowed)
{
actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
}
}
}
当您使用带有声明的令牌(例如oAuth)时,该令牌将拥有一个名为“角色”的声明。当您的网站收到此令牌时,它将负责创建主体对象并根据角色声明中的角色设置角色。然后,您将能够继续使用标准的'AuthorizeAttribute'或您自定义的'AuthLogAttribute' –
我对这方面的知识很少。你可以将我引导到任何详细讨论和指导的文章。谢谢 –