将参数传递给 Identity Server 4 登录页面

Nob*_*ara 1 asp.net-mvc asp.net-identity asp.net-core identityserver4

我有一个使用 IDSVR4 进行身份验证的客户端应用程序。我需要在客户端的会话或其他客户端数据存储机制中存储用户的用户名(与通过 IDSVR 登录的过程不同),以便用户每次登录时都不必输入详细信息特定浏览器。如何将用户名从客户端传递到身份服务器?

Esp*_*dbø 5

您可以将用户名添加到请求参数中。如果您在客户端使用 asp.net,您可以使用通知事件,RedirectToIdentityProvider然后将您的用户名添加到 ProtocolMessage。像这样的东西:

RedirectToIdentityProvider = context =>
{
    context.ProtocolMessage.Parameters['username'] = "John";
    return Task.CompletedTask;
}
Run Code Online (Sandbox Code Playgroud)

您添加到参数的任何值都可以通过IIdentityServerInteractionService方法在 IdentityServer 中访问GetAuthorizationContextAsync

像这样,在您的 IdentityServer 控制器中:

public async Task<IActionResult> Login(string returnUrl){
    var context = await _interaction.GetAuthorizationContextAsync(returnUrl);
    var username = context.Parameters['username'];
    ...
}
Run Code Online (Sandbox Code Playgroud)