亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Asp .Net Core 為什么即使用戶未通過身份驗證也會調用我的授權處理程序?

Asp .Net Core 為什么即使用戶未通過身份驗證也會調用我的授權處理程序?

C#
人到中年有點甜 2023-05-13 16:21:56
我有一個問題,如果我手動轉到登錄后的頁面,我會在我的一個保單句柄中遇到異常,因為它找不到索賠。為什么會發生這種情況而不是將用戶重定向到登錄頁面,因為他沒有通過身份驗證?我什至設置了身份驗證方案,因為我所有的頁面都有[Authorize("scheme"), Policy = "policy"]這是我的整個啟動代碼public class Startup{    public Startup(IConfiguration configuration)    {        Configuration = configuration;    }    public IConfiguration Configuration { get; }    // This method gets called by the runtime. Use this method to add services to the container.    public void ConfigureServices(IServiceCollection services)    {        services.AddAuthentication()         .AddCookie("ProductionAuth", options =>         {             options.ExpireTimeSpan = TimeSpan.FromDays(1);             options.LoginPath = new PathString("/Production/Index");             options.LogoutPath = new PathString("/Production/Logout");             options.AccessDeniedPath = new PathString("/Production/AccessDenied/");             options.SlidingExpiration = true;         })        .AddCookie("AdministrationAuth", options =>        {            options.ExpireTimeSpan = TimeSpan.FromDays(1);            options.LoginPath = new PathString("/Administration/Index");            options.LogoutPath = new PathString("/Administration/Logout");            options.AccessDeniedPath = new PathString("/Administration/AccessDenied/");            options.SlidingExpiration = true;        });        services.AddAuthorization(options =>        {            options.AddPolicy("HasArranqueActivo", policy =>                policy.RequireAuthenticatedUser()                .AddAuthenticationSchemes("ProductionAuth")                .Requirements.Add(new HasArranqueActivoRequirement()            ));
查看完整描述

3 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

文檔中有一條重要說明可以解決此問題:


即使身份驗證失敗,也會調用授權處理程序。


在您的情況下,身份驗證失敗但您的IsParagemNotOnGoingHandler'HandleRequirementAsync仍在被調用。要解決此問題,您只需讓您的處理程序實現對丟失的聲明更具彈性。這是完整性的示例:


protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, IsParagemNotOnGoingRequirement requirement)

{

    if (!context.User.HasClaim(c => c.Type == ClaimTypes.PrimarySid))

        return Task.CompletedTask;


    ...

}

Convert.ToInt32對于聲明的值不可轉換為int.


查看完整回答
反對 回復 2023-05-13
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

文檔中有一條重要說明可以解決此問題:


即使身份驗證失敗,也會調用授權處理程序。


在您的情況下,身份驗證失敗但您的IsParagemNotOnGoingHandler'HandleRequirementAsync仍在被調用。要解決此問題,您只需讓您的處理程序實現對丟失的聲明更具彈性。這是完整性的示例:


protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, IsParagemNotOnGoingRequirement requirement)

{

    if (!context.User.HasClaim(c => c.Type == ClaimTypes.PrimarySid))

        return Task.CompletedTask;


    ...

}

Convert.ToInt32對于聲明的值不可轉換為int.


查看完整回答
反對 回復 2023-05-13
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

在您的 中AuthorizationHandler,您可以通過訪問屬性來檢查用戶是否已通過身份驗證,context.User.Identity.IsAuthenticated如下所示:


protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, YourRequirementType requirement)

{

    if (context.User.Identity == null || !context.User.Identity.IsAuthenticated)

    {

        _logger.LogDebug("Authorization Failed: User not authenticated.");

        context.Fail(new AuthorizationFailureReason(this, $"User not authenticated"));

        return Task.CompletedTask;

    }


查看完整回答
反對 回復 2023-05-13
  • 3 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號