简单 IdentityServer3/OpenIdConnect 解决方案不起作用 - HTTP 401.0 - 未经授权

Simple IdentityServer3/OpenIdConnect solution not working - HTTP 401.0 - Unauthorized

我是 IdentityServer 和 OpenId Connect 的新手。我正在使用 .NET Framework 6 和 IdentityServer3 以及 OpenId Connect。我已经完成了 IdentityServer3 文档的概述部分中的三个演练 - 使用控制台客户端、MVC 客户端和 JavaScript 客户端 - 并将这些工作解决方案作为模型。我现在正尝试使用一个简单的 MVC 客户端构建我自己的身份验证服务以进行测试。它不工作。它不工作的症状是,MVC 客户端“关于”页面(使用 Home 控制器中的 [Authorize] 属性保护)报告 HTTP 401.0 - 未经授权,而没有将用户重定向到登录页面。我查看了 Chrome 开发人员工具中的网络流量,发现 /Home/About 的初始请求返回 HTTP 401,而不是预期的 HTTP 302 重定向。

我的 IdentityServer3 配置如下所示:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // TODO: Replace use of InMemoryUsers with custom Orvis UserService
        app.UseIdentityServer(new IdentityServerOptions
        {
            SiteName = "MyClient Authentication Service",
            SigningCertificate = LoadCertificate(),
            Factory = new IdentityServerServiceFactory()
            .UseInMemoryClients(Clients.Get())
            .UseInMemoryScopes(Scopes.Get())
            .UseInMemoryUsers(Users.Get().ToList()),
            RequireSsl = true
        });
    }

    X509Certificate2 LoadCertificate()
    {
        // TODO: Get real signing certificate?
        return new X509Certificate2(string.Format(@"{0}\bin\idsrv3test.pfx", AppDomain.CurrentDomain.BaseDirectory), "idsrv3test");
    }
}

GetClients 和 GetScopes 代码如下所示:

public static IEnumerable<Client> Get()
{
    // TODO: Replace hard-coded implementation of Clients.Get() with a configuration-driven implementation
    return new List<Client>
    {
        new Client
        {
            Enabled = true,
            ClientId = "mvc-sample",
            ClientName = "MVC Sample Client",
            RequireConsent = false,
            Flow = Flows.Implicit,
            RedirectUris = new List<string> { "http:/localhost:37320" },
            AllowedCorsOrigins = new List<string> { "http://localhost:37320/" },
            AllowedScopes = new List<string> { "orvis-services", "orvis-shopper-service" }
        }
    };
}

public static IEnumerable<Scope> Get()
{
    return new List<Scope>
    {
        new Scope
        {
            Name = "all-services",
            DisplayName = "All Services", 
            Description = "Access to all microservices",
            Type = ScopeType.Resource
        }, 
        new Scope
        {
            Name = "shopper-service",
            DisplayName = "Shopper Service",
            Description = "Access to the Shopper microservice", 
            Type = ScopeType.Resource
        }
    };
}

我的客户端配置如下所示:

public void Configuration(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = "Cookies"
    });

    app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
    {
        Authority = "https://localhost:44332",
        ClientId = "mvc-sample",
        RedirectUri = "http://localhost:37320",
        ResponseType = "id_token",
        SignInAsAuthenticationType = "Cookies"
    });
}

因此,与 IdentityServer3 "Getting Started: MVC Authentication and Web APIs" 演练非常相似。一个区别是演练在同一个项目中有客户端和服务器代码,我将它们分成不同的项目。

由于开发工具网络选项卡显示对 /Home/About 的请求带有 401.0 响应而不是预期的 302 响应,我怀疑 cilent 设置有问题。但是,除了浏览器开发工具之外,我不确定去哪里查看底层细节。

任何建议表示赞赏。

问题是我用作模型的 IdentityServer3 遍历在一个项目中具有 IdentityServer 和 MVC 客户端。当我分别创建它们时,我将 Microsoft.Owin.Host.Systemweb 和 IdentityServer3 包添加到 IdentityServer 项目,将 Microsoft.Owin.Security.Cookies 和 Microsoft.Owin.Security.OpenIdConnect 包添加到 MVC 客户端项目。原来MVC客户端项目也需要Microsoft.Owin.Host.Systemweb包。没有它,永远不会调用 Startup.Configuration() 方法。