如何将Identity Server与.NET Core 2.1一起使用?

w00*_*977 1 c# asp.net-core identityserver4

我试图让Identity Server在ASP.NET Core 2.1项目上工作,我已按照这里的说明进行操作,但是,我意识到这些是针对ASP.NET Core 2.0的.

MVC客户端中的Startup如下所示:

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
    .AddCookie("Cookies")
    .AddOpenIdConnect("oidc", options =>
    {
        options.SignInScheme = "Cookies";
        options.Authority = "http://localhost:5000";
        options.RequireHttpsMetadata = false;
        options.ClientId = "mvc";
        options.ClientSecret = "secret";
        options.ResponseType = "code id_token";
        options.SaveTokens = true;
        options.GetClaimsFromUserInfoEndpoint = true;
        options.Scope.Add("api1");
        options.Scope.Add("offline_access");
    });
Run Code Online (Sandbox Code Playgroud)

使用ASP.NET Core 2.1,可以在此处访问标识组件:http://localhost/Identity/Account/Login.上面的代码重定向到:http://localhost/Account/Login.我的第一个想法是更换以下行:

options.Authority = "http://localhost:5000";
Run Code Online (Sandbox Code Playgroud)

有:

options.Authority = "http://localhost:5000/Identity";
Run Code Online (Sandbox Code Playgroud)

但是,我接到一个错误说:

IOException:IDX10804:无法从以下位置检索文档:' http:// localhost:5000/Identity/.well-known/openid-configuration '.".

这是因为路径必须是:' http:// localhost:5000/.well-known/openid-configuration '.

我可以通过路由修复此问题吗?我相信如果我确保所有请求:http://localhost:5000/Account/Login映射到http://localhost:5000/Identity/Account/Login,那么它将解决问题.这是正确的,路线会是什么样子?我无法获得使用区域(身份)的路线.

pok*_*oke 6

使用OpenID Connect时,您没有在Web应用程序上使用登录表单.您将登录职责委派给OpenID Connect提供程序.在您的情况下,即IdentityServer,它在单独的应用程序中运行.

因此,您需要在此处配置您的Web应用程序:权限 IdentityServer的根URL,因此"http://localhost:5000"应该是正确的.您需要配置的是IdentityServer,如果它在没有用户登录的情况下接收授权请求,则会将其重定向到正确的端点.

您可以在StartupIdentityServer应用程序中执行此操作,您可以在其中添加服务:

services.AddIdentityServer(options =>
{
    options.UserInteraction.LoginUrl = "/Identity/Account/Login";
    options.UserInteraction.LogoutUrl = "/Identity/Account/Logout";
})
Run Code Online (Sandbox Code Playgroud)