【问题标题】:IdentityServer4 and .netcore WebApp/WebAPI cookie authentication/authorizationIdentityServer4 和 .netcore WebApp/WebAPI cookie 身份验证/授权
【发布时间】:2018-11-10 14:17:59
【问题描述】:

我有三个应用程序,即(IdentityServer4 App、.Net Core2.0 WebApp、.NetCore2.0 WebAPI)

当我打开未经身份验证的 web 应用程序时,它会导航到我提供凭据的身份服务器。成功验证后,它会导航回 webapp,并使用所需的 cookie。到这里为止一切都很好。

现在在 webapp 中,我正在调用 webapi(使用 webapp 中的身份服务器设置的 cookie),但每次它都返回 401 未授权。

webapp 中的代码示例:

services.AddAuthentication(options =>
   {
      options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
      options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
  })
  .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
        {
            o.Cookie.Name = Config.CookieName;
            o.Cookie.SameSite = SameSiteMode.None;
        })
  .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
        {
            options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

            options.Authority = Config.IdentityUrl;
            options.RequireHttpsMetadata = false;
            options.ClientId = Config.ClientId;
            options.SaveTokens = true;
        });

以及配置服务方法ConfigureServices中WebAPI中使用的代码示例:

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o => {
    o.Cookie.Name = Config.CookieName;
    o.Cookie.SameSite = SameSiteMode.None;
    o.Events = new CookieAuthenticationEvents()
    {
        OnRedirectToLogin = redirectContext =>
        {
            redirectContext.HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
            return Task.CompletedTask;
        }
    };
})
.AddIdentityServerAuthentication(options =>
{
    options.Authority = Config.IdentityUrl;
    options.RequireHttpsMetadata = false;
    options.ApiName = Config.ApiName;
});

我在Configure 方法中有app.UseAuthentication() 方法

我的感觉与 session-id 可能有关。如果是这样,请提供帮助,如果不是,那么您可能认为做得不对,请提供帮助。

我追踪了日志,它只显示了以下内容:

Cookie 未通过身份验证。失败消息:取消保护票证失败。

身份验证 Cookie 被挑战。

任何帮助将不胜感激。

【问题讨论】:

    标签: ajax web-applications asp.net-web-api2 asp.net-core-2.0 identityserver4


    【解决方案1】:

    这是神奇的代码行。添加在

    配置服务

    之前的方法

    services.AddAuthentication

    这是未验证 cookie 的原因。

    services.AddDataProtection().PersistKeysToFileSystem(PersistKeysLocation.GetKeyRingDirInfo()) .SetApplicationName(Config.ApplicationName);

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 2019-11-16
      • 2016-07-30
      • 2015-12-24
      • 2020-03-07
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多