Все работает на Localhost. Это происходит только на хостинном сервере. Направляется нормально, как если бы авторизация не применяется, и метод поднимает исключение, потому что идентификатор пользователя является null (не аутентифицирован)
Это метод, который я использую:
Код: Выделить всё
[HttpGet("get-profile-information")]
[Authorize]
public async Task GetProfileInformation()
{
var userId = User.Identity.Name;
var GetProfileDataByUserIdAsyncResult = await
profileManagerService.GetProfileDataByUserIdAsync(userId);
return GetProfileDataByUserIdAsyncResult.Match(Ok, BadRequest);
}
< /code>
Мои настройки аутентификации: < /p>
serviceCollection.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "id",
RoleClaimType = "role",
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = configuration["JwtConfig:Issuer"],
ValidAudience = configuration["JwtConfig:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JwtConfig:Key"]))
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
context.Token = context.Request.Cookies[configuration["JwtConfig:AccessToken"]];
return Task.CompletedTask;
}
};
}).AddGoogle(options =>
{
options.ClientId = configuration["Authentication:Google:ClientId"];
options.ClientSecret = configuration["Authentication:Google:ClientSecret"];
options.CallbackPath = "/signin-google";
options.SaveTokens = false;
}).AddFacebook(options =>
{
options.ClientId = configuration["Authentication:Facebook:AppId"];
options.ClientSecret = configuration["Authentication:Facebook:AppSecret"];
options.CallbackPath = "/signin-facebook";
options.SaveTokens = false;
});
< /code>
Я даже попробовал это: < /p>
[HttpGet("get-profile-information")]
[Authorize]
public async Task GetProfileInformation()
{
return Ok();
var userId = User.Identity.Name;
if (userId is null || !User.Identity.IsAuthenticated)
return Unauthorized();
var GetProfileDataByUserIdAsyncResult = await
profileManagerService.GetProfileDataByUserIdAsync(userId);
return GetProfileDataByUserIdAsyncResult.Match(Ok, BadRequest);
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... ted-server