Das Attribut autorisieren, funktioniert nicht auf gehosteten Server
Posted: 24 Feb 2025, 04:14
Ich habe eine ASP.NET -Web -API, die auf Smartter ASP gehostet wird, aber aus irgendeinem Grund funktioniert das [autorize] Attribut nicht und auf den Endpunkt wird unabhängig davon zugegriffen, ob der Benutzer authentifiziert wurde oder nicht und eine Ausnahme erhöht wird . < /p>
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:
und es gab eine Antwort von 200 zurück, als ob der Benutzer authentifiziert wäre (was nicht der Fall ist).
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);
}