授权属性无法在托管服务器上工作

问题描述 投票:0回答:1
i我在更智能的ASP上托管了ASP.NET Web API,但是由于某种原因,

[Authorize]

属性不起作用,并且无论用户是否经过身份验证,并且提高了例外。
在Local主机上的全部功能仅在托管服务器上发生。

我期望的是返回未经授权的响应,而不是正常调用的方法,好像没有应用授权,并且该方法提高了例外,因为用户的ID是无效的(未进行认证)

这是我正在使用的方法:

[HttpGet("get-profile-information")] [Authorize] public async Task<IActionResult> GetProfileInformation() { var userId = User.Identity.Name; var GetProfileDataByUserIdAsyncResult = await profileManagerService.GetProfileDataByUserIdAsync(userId); return GetProfileDataByUserIdAsyncResult.Match<IActionResult, ProfileDataDto>(Ok, BadRequest); }

我的身份验证设置:

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;
        });

我甚至尝试过:

[HttpGet("get-profile-information")]
[Authorize]
public async Task<IActionResult> 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<IActionResult, ProfileDataDto>(Ok, BadRequest);
}

它返回了200个响应,就好像用户是身份验证(情况并非如此)。

我认为您错过了添加中间件。
在您的program.cs中,在添加控制器/端点之前,您需要添加以下

app.UseAuthentication(); app.UseAuthorization();
c# .net asp.net-core asp.net-web-api
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.