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

标签 c# asp.net-core identityserver4

我正在尝试让 Identity Server 在 ASP.NET Core 2.1 项目上运行,并且我已按照说明进行操作 here ,但是,我意识到这些是针对 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");
    });

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

options.Authority = "http://localhost:5000";

与:

options.Authority = "http://localhost:5000/Identity";

但是,我收到一条错误消息:

IOException: IDX10804: Unable to retrieve document from: '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 ,然后它将解决问题。这是正确的吗?路线是什么样的?我无法获得使用区域(身份)的路线。

最佳答案

当您使用 OpenID Connect 时,您没有网络应用程序上的登录表单。您将登录责任委托(delegate)给 OpenID Connect 提供商。在您的情况下,这是在单独的应用程序中运行的 IdentityServer。

因此,您需要在此处配置的不是您的 Web 应用程序:授权您的 IdentityServer 的根 URL,因此 "http://localhost:5000" 在那里应该是正确的。相反,您需要配置的是 IdentityServer,以使其在用户未登录的情况下收到授权请求时重定向到正确的端点。

您可以在您的 IdentityServer 应用程序的 Startup 中添加服务:

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

关于c# - 如何将 Identity Server 与 .NET Core 2.1 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51728881/

相关文章:

c# - 使用 PHP 和 c# 的 Mcrypt 和 base64

c# - 我可以在内部制作 ASP.NET Core Controller 吗?

c# - 扩展 Entity Framework 将列添加到 Identity Server 4 客户端表

authentication - identityserver4 上用户的多个外部客户端

c# - 如何向ffmpeg提供登录用户名和密码?

c# - 禁用 HttpWebRequest 的图像下载

c# - 在 ASP.NET Core 中访问 Program.Main 中的环境名称

visual-studio-2015 - Visual Studio 2015 (ASP.NET Core) 编译错误 Error : Microsoft. DotNet.Common.Targets - 已添加具有相同键的项目

c# - 身份服务器 4 AddInMemoryIdentityResources

c# - Visual Studio 显示不存在的错误