Utilisation du client OIDC de here. Et serveur de démonstration de here et here.Comment activer [Autoriser] sur TestController sur IdentityServer4 à des fins de revendication Contrôleur CRUD
J'ai le contrôleur suivant sur le IdentityServer lui-même:
[Route("api/Test")]
//[Authorize]
[Authorize(ActiveAuthenticationSchemes = "Bearer")]
public class TestController : ControllerBase
{
public IActionResult Get()
{
var claims = User.Claims.Select(c => new { c.Type, c.Value });
return new JsonResult(claims);
}
}
Si je commente sur les deux attributs [Authorize]
, j'atteindre le TestController.
Si je viens d'utiliser [Authorize]
, je reçois l'erreur suivante:
GET http://localhost:5000/api/Test dashboard:1 XMLHttpRequest cannot load http://localhost:5000/api/Test . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:4200 ' is therefore not allowed access. The response had HTTP status code 500.
Et si je viens d'utiliser [Authorize(ActiveAuthenticationSchemes = "Bearer")]
je reçois:
GET http://localhost:5000/api/Test dashboard:1 XMLHttpRequest cannot load http://localhost:5000/api/Test . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:4200 ' is therefore not allowed access. The response had HTTP status code 500. dashboard:1 XMLHttpRequest cannot load http://localhost:5000/api/Test . Redirect from ' http://localhost:5000/api/Test ' to ' http://localhost:5000/Account/Login?ReturnUrl=%2Fapi%2FTest ' has been blocked by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.
Le code que j'utilise pour appeler le point final de la OIDC le client est:
test() {
this.authService.mgr.getUser()
.then(user => {
// this.http.get('https://api.identityserver.io/identity',
this.http.get('http://localhost:5000/api/Test',
{ headers: new Headers({ 'Authorization': `${user.token_type} ${user.access_token}`}) })
.subscribe(res => {
console.log(res.json());
});
});
}
Je suis en mesure d'appeler avec succès https://api.identityserver.io/identity avec ceci.
Ceci est mon CorsPolicyHelper:
public class DemoCorsPolicy : ICorsPolicyService
{
public Task<bool> IsOriginAllowedAsync(string origin)
{
return Task.FromResult(true);
}
}
Et c'est là sa forme appelée Startup:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AuthDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
...
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Resources.GetIdentityResources())
.AddInMemoryApiResources(Resources.GetApiResources())
.AddInMemoryClients(Clients.GetClients())
.AddAspNetIdentity<ApplicationUser>();
services.AddTransient<ICorsPolicyService, DemoCorsPolicy>();
}
Le but ultime est d'effectuer des opérations CRUD sur les autorisations/demandes. Je suis actuellement coincé sur cette tâche apparemment anodin d'avoir une autorisation contrôleur protégé:/