2016-01-21 42 views

回答

1

This article很好地覆蓋它。

本質上,[Authorize(Roles = "Blah")]只是爲了向後兼容,政策是未來的方向。

我不會剪切和粘貼,但本質上,這個動作:

[Authorize("SalesOnly")] 
public IActionResult DoSalesyStuff() 
{ /* .. */ } 

有線在Startup.ConfigureServices:

// only allow authenticated users 
var defaultPolicy = new AuthorizationPolicyBuilder() 
    .RequireAuthenticatedUser() 
    .Build(); 

services.AddMvc(setup => 
{ 
    setup.Filters.Add(new AuthorizeFilter(defaultPolicy)); 
}); 

services.AddAuthorization(options => 
{ 
    // inline policies 
    options.AddPolicy("SalesOnly", policy => 
    { 
     policy.RequireClaim("department", "sales"); // Some policy 
    }); 
}); 

筆者把他們的full example code on github和來源爲security middleware is here