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

时间:2018-12-05 13:29:49

标签: asp.net-mvc asp.net-core asp.net-identity identityserver4

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

2 个答案:

答案 0 :(得分:2)

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

RedirectToIdentityProvider = context =>
{
    context.ProtocolMessage.Parameters['username'] = "John";
    return Task.CompletedTask;
}

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

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

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

答案 1 :(得分:1)

您可以将自定义参数传递给授权端点,有关代码示例,请参阅我的回复here

如果您不使用OpenID Connect OWIN中间件,则可以直接将自定义参数放入授权请求中:

http://localhost:xxxx/account/login?returnUrl=/connect/authorize/callback?client_id=mvc2&redirect_uri=http%3A%2F%2Flocalhost%3A49459%2Fsignin-oidc&response_type=code%20id_token&scope=openid%20profile%20api1%20offline_access&response_mode=form_post&nonce=xxxx&state=xxxx

在身份服务器端,您可以解析returnUrl并轻松获取参数。