這是自定義的授權從數據庫檢查權限。 例如,你有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' –
我對這方面的知識很少。你可以將我引導到任何詳細討論和指導的文章。謝謝 –