ASP.NET Core Identity Server4-如何使用客户端登录页而不是Identity Server登录



我的项目中有2个API、1个MVC Web应用程序和IdentityServer应用程序。

本地端口:

IdentityServer:https://localhost:1000

API 1:https://localhost:2000

API 2:https://localhost:3000

客户:https://localhost:4000

就MVC Web应用程序而言,其Startup.cs身份验证服务是这样的;

services.AddAuthentication(_ =>
{
DefaultScheme = "...";
DefaultChallengeScheme = "oidc";
})
.AddCookie("...", options => options.AccessDeniedPath = "/home/accessdenied")
.AddOpenIdConnect("oidc", _ =>
{
_.SignInScheme = "...";
_.Authority = "https://localhost:1000";
_.ClientId = "...";
_.ClientSecret = "...";
_.ResponseType = "code id_token";
_.GetClaimsFromUserInfoEndpoint = true;
_.SaveTokens = true;
_.Scope.Add("offline_access");
_.Scope.Add("...");
_.Scope.Add("...");
_.Scope.Add("...");
});

在MVC Web应用程序中,有一个像这样的控制器;

[Authorize]
public async Task<IActionResult> PayMoney()
{
var authenticationProperties = (await HttpContext.AuthenticateAsync()).Properties.Items;
string accessToken = authenticationProperties.FirstOrDefault(x => x.Key == ".Token.access_token").Value;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
HttpResponseMessage responseMessage = await httpClient.GetAsync("https://localhost:2000/api/.....");
string total = await responseMessage.Content.ReadAsStringAsync();
return View();
}

在这一点上,我一起运行应用程序。之后,我打电话给PayMoney(如上(,然后项目将我重定向到带有ReturnUrl的Identity Server登录页面(帐户/登录(,因为我没有经过身份验证和授权。

但我想使用MVC应用程序的登录页面,而不是Identity Server项目的Quickstart.UI登录页面

有可能吗?当我研究时,人们说我应该使用"资源所有者密码",但这对我的项目来说并不安全。。。

有人知道这件事吗

在OpenID Connect中,您不应该让客户端应用程序看到/触摸用户的用户名/密码。相反,根据设计,应该将用户重定向到身份提供者进行身份验证。

作为一个客户,我不相信会把我的凭据交给个别客户,相反,你只想把它交给你作为用户信任的人。

在IdentityServer中,如果您想显示自己的登录屏幕,则必须通过在项目中安装NuGet包来实现OpenID Connect。Microsoft.AspNetCore.Authentication.OpenIdConnect。整个集成过程在本教程中给出-使用ASP.NET Core Identity和MongoDB作为数据库的IdentityServer[详细指南]。

您也可以从这个在.NET 5.0中实现的GitHub存储库下载代码。

最新更新