【问题标题】:Can't access Hangfire Dashboard using Authorization boilerplate code无法使用授权样板代码访问 Hangfire 仪表板
【发布时间】:2021-07-27 23:26:04
【问题描述】:

我可以从本地环境访问 Hangfire 仪表板,但无法从已部署的服务器访问它。初步搜索显示我需要启用授权。所以我有,根据非常简单的样板示例。

首先,我创建了这个类,Authorize 方法应该强制批准:

public class HangFireAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize([NotNull] DashboardContext context)
    {
        return true;

        //var httpContext = context.GetHttpContext();

        //// Allow all authenticated users to see the Dashboard (potentially dangerous).
        //return httpContext.User.Identity.IsAuthenticated;
    }
}

然后我添加了配置:

app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
    Authorization = new[] { new HangFireAuthorizationFilter() }
});

但在我部署并尝试访问 /hangfire 页面后,我在日志中看到了这一点:

fail: Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery[7]
  An exception was thrown while deserializing the token.
  Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The antiforgery token could not be decrypted.
   ---> System.Security.Cryptography.CryptographicException: The key {450e662b-997d-4b41-b70b-3290373a73a7} was not found in the key ring.
     at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
     at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
     at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
     at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
     --- End of inner exception stack trace ---
     at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
     at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.GetCookieTokenDoesNotThrow(HttpContext httpContext)

还有什么我没有考虑的吗?

【问题讨论】:

    标签: asp.net hangfire


    【解决方案1】:

    我得到它的工作,但解决方案不寻常,与 documentation 不匹配。

    DashboardOptions 对象从UseHangfireDashboard() 方法移动到MapHangfireDashboard() 方法有效。

    endpoints.MapHangfireDashboard("/hangfire", new DashboardOptions
    {
        Authorization = new[] { new HangFireAuthorizationFilter() }
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2016-12-08
    • 2018-08-25
    • 2016-03-27
    相关资源
    最近更新 更多