Alles funktioniert auf localhost Dies geschieht nur auf dem gehosteten Server. Die Methode wird normal aufgerufen, als ob keine Autorisierung angewendet wird und die Methode eine Ausnahme erhöht, da die ID des Benutzers null ist (nicht authentifiziert)
Dies ist die Methode, die ich verwende:
Code: Select all
[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>
Meine Authentifizierungseinstellungen: < /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>
Ich habe dies sogar versucht: < /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);
}