【问题标题】:IdentityServer4 Authentication in .NET Framework WebApi Owin UseOpenIdConnectAuthentication.NET Framework WebApi Owin UseOpenIdConnectAuthentication 中的 IdentityServer4 身份验证
【发布时间】:2020-07-28 12:32:50
【问题描述】:

所以,我在一个单独的应用程序中设置了 IdentityServer4,WebConfigurationManager.AppSettings["IdentityServer:Authority"] 将在代码中引用该应用程序。

我有一个 .NET Framework 4.6.1 WebApi 应用程序,需要点击此应用程序来验证请求。我在 IdentityServer4 应用程序中将 WebApi 应用程序设置为 ApiResource“mywebppi”。

要在 .NET Core 应用程序中执行此操作,只需在 Startup.cs 中添加以下代码即可:

services.AddAuthentication(Authentication.Bearer)
        .AddJwtBearer(Authentication.Bearer, options =>
        {
            options.Authority = Configuration.GetSection("IdentityServer:Authority").Value;
            options.RequireHttpsMetadata = false;
            options.Audience = "mywebppi";
        });

但是,由于这是一个 .NET Framework 4.6.1 WebApi 应用程序,我不能这样做。我读到您可以使用 IdentityServer3.AccessTokenValidation 和 Owin 来执行此操作,但是由于 IdentityServer3 nuget 包依赖于 IdentityModel 1.9.2 到 2.0.0,并且我的 WebApi 应用程序已经使用 IdentityModel 3.10.10 并且不能降级那么远,所以这是不是一个选项。

我在其他地方找到了有关尝试使用 Owin 的 UseOpenIdConnectAuthentication 的信息。我试过这个:

[assembly: OwinStartup(typeof(MyWebApi.Startup))]

namespace MyWebApi
{
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {    
                app.SetDefaultSignInAsAuthenticationType("Bearer");
                app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
                {
                    Authority = WebConfigurationManager.AppSettings["IdentityServer:Authority"],
                    Scope = "mywebapi",
                    ResponseType = "id_token",
                    UseTokenLifetime = false,
                    RequireHttpsMetadata = false,
                    AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive,
                    SignInAsAuthenticationType = "Bearer"
                });
            }
        }
}

但是,我不断收到“消息”:“此请求的授权已被拒绝。”

【问题讨论】:

    标签: asp.net-web-api identityserver4 jwt-auth owin-middleware .net-4.6.1


    【解决方案1】:

    我建议你在 web api 上打开 OWIN 日志以便能够看到问题的详细信息,here 是一个将日志设置为 NLog 的示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-12
      • 2014-08-19
      • 2014-10-19
      • 1970-01-01
      • 2017-12-17
      • 2014-02-20
      • 1970-01-01
      • 2015-01-14
      相关资源
      最近更新 更多