Я внедряю Auth0 для использования в своем приложении ASP.NET Core 2.1 с интерфейсом React и не могу выполнять вызовы API, используя токен, полученный от Auth0. Я продолжаю получать несанкционированную ошибку.
Вот код метода ConfigureServices()
в моем Startup.cs
:
services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions => {
jwtOptions.Authority = "https://myapp.auth0.com/";
jwtOptions.Audience = "https://myapp.com/api/";
});
string domain = $"https://myapp.auth0.com/";
services.AddAuthorization(options =>
{
options.AddPolicy("read:data", policy => policy.Requirements.Add(new HasScopeRequirement("read:data", domain)));
});
services.AddSingleton<IAuthorizationHandler, HasScopeHandler>();
У меня есть следующее для обработки области:
public class HasScopeRequirement : IAuthorizationRequirement
{
public string Issuer { get; }
public string Scope { get; }
public HasScopeRequirement(string scope, string issuer)
{
Scope = scope ?? throw new ArgumentNullException(nameof(scope));
Issuer = issuer ?? throw new ArgumentNullException(nameof(issuer));
}
}
public class HasScopeHandler : AuthorizationHandler<HasScopeRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasScopeRequirement requirement)
{
// If user does not have the scope claim, get out of here
if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer))
return Task.CompletedTask;
// Split the scopes string into an array
var scopes = context.User.FindFirst(c => c.Type == "scope" && c.Issuer == requirement.Issuer).Value.Split(' ');
// Succeed if the scope array contains the required scope
if (scopes.Any(s => s == requirement.Scope))
context.Succeed(requirement);
return Task.CompletedTask;
}
}
Также в методе Configure()
у меня есть app.UseAuthentication();
Вот код в моем сервисе auth0.js
:
auth0 = new auth0.WebAuth({
domain: 'myapp.auth0.com',
clientID: 'client_id_copied_from_application_settings_on_auth0',
redirectUri: 'http://localhost:49065/member/callback',
audience: 'https://myapp.com/api/',
responseType: 'token id_token',
scope: 'openid'
});
Мой интерфейс успешно получает access_token
, expires_at
и id_token
. Используя Postman, я делаю вызов API к одному из моих методов API, но продолжаю получать ошибку 401 Unauthorized
. Ниже я публикую скриншот того, что я вижу в Postman. Похоже, это не нравится публике.
Любая идея, что я делаю неправильно?
https://
перед моим доменом? В настоящее время мой доменmycompanyname.auth0.com
. Может ли это быть проблемой? 05.09.2018https://
- кроме того, вы хотите убедиться, что включен полный URL-адрес обратного вызова. 05.09.2018