【问题标题】:Configure IdentityServer4 with OWIN self-hosted WebApi使用 OWIN 自托管 WebApi 配置 IdentityServer4
【发布时间】:2018-05-14 11:06:09
【问题描述】:

我正在尝试使用 IdentityServer4 创建身份服务。该服务使用 OWIN (.Net Fx 4.7.x) 自托管。这是我迄今为止尝试过的。

尝试#1:使用documentation 中的示例: 但是,所有示例均基于 .Net 核心。复制 app.UseIdentityServer(); 等代码不起作用,因为在示例中,app 的类型为 IApplicationBuilder,而在 OWIN 自托管应用程序中,我们有 IAppBuilder

// Startup.cs
public void Configuration(IAppBuilder app)
{
    // Configure Web API for self-host. 
    HttpConfiguration config = new HttpConfiguration();
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    // Configure Unity IOC

    app.UseIdentityServer(); //<--doesn't compile
    app.UseWebApi(config);
}

尝试#2:手动注册 IdentityServer 中间件:我尝试通过查看源代码手动注册所需的中间件。如下所示:

//Startup.cs
public void Configuration(IAppBuilder app)
{
    ...
    // Configure Unity IOC

    app.Use<IdentityServerMiddleware>(
        config.DependencyResolver.GetService(typeof(ILogger<IdentityServerMiddleware>)));
    app.UseWebApi(config);
}

这也不起作用,因为Main 方法在使用WebApp.Start&lt;Startup&gt;(baseAddress); 盯着 WebApp 时会引发以下错误

System.Web.Http.Owin.HttpMessageHandlerAdapter 和 Microsoft.AspNetCore.Http.RequestDelegate 之间没有可用的转换。 参数名称:签名

如何正确配置它?我知道在这种情况下我可以使用 IdentityServer3,但我热衷于使用 IdentityServer4,因为不再维护 IdentityServer3。

【问题讨论】:

  • 不能,身份服务器 4 仅用于 dotnet 核心。您可以使用 net framework 或不选择创建一个单独的项目作为 dotnet core,然后这将是您所有应用程序进行身份验证的终点。否则你必须使用身份服务器 3。
  • 你的问题有点不清楚。您是否尝试使用身份服务器 4 为 .net 4.7 创建身份服务器?或者您是否尝试使用 .net 4.7 连接到身份服务器?
  • @DaImTo 很抱歉造成混乱。更新了问题。我正在尝试创建身份服务。

标签: c# asp.net owin identityserver4 self-host-webapi


【解决方案1】:

直接来自Identity server 4的文档

IdentityServer4 是一个适用于 ASP.NET Core 2 的 OpenID Connect 和 OAuth 2.0 框架。

Identity server 4 仅适用于 Asp.Net core 2.0。您将无法使用它来使用旧的 ASP.Net (Owin/KATANA) 创建身份服务器,我建议您切换到 ASP.NET Core 2.0。

如 cmets 中所述,您可以返回 Identity Server 3,但不再支持此功能,因此如果出现任何问题,可能不会有任何安全更新。因此,我不会亲自在新的生产产品中使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 2015-10-06
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多