你也許應該將在這個文件的請求,但讓你開始可以實現IAuthenticationFilter
,登記,然後用類似裝飾你的控制器:[Authorize(Roles = "CanCreateCustomer")]
public class CustomAuthenticationAttribute : Attribute, System.Web.Http.Filters.IAuthenticationFilter
{
public bool AllowMultiple
{
get
{
return true;
}
}
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
context.Principal = //get principal here, based on your implementation
}
public async Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
await Task.FromResult(0);
}
}
註冊它:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Filters.Add(new CustomAuthenticationAttribute());
}
}
我有在生產實施在此之前發佈的概念證明索賠授權。您可以查看過濾器:https://github.com/trailmax/ClaimsAuthorisation/blob/master/ClaimsAuth/Infrastructure/Identity/ClaimsAuthorisationFilter.cs,並想知道其他回購協議。 – trailmax