用户调用HttpContext.SignInAsync后退出IdentityServer4

时间:2018-08-08 03:15:41

标签: authentication identityserver4

this问题之后,我遇到了一个有趣的难题。

我已经基于IdentityServer4快速入门构建了IdentityServer。当客户端将用户定向到IdentityServer,并且用户进行身份验证时,IdentityServer上的AccountController执行_signInManager.PasswordSignInAsync,并且用户登录到IdentityServer。

这可以通过打开另一个浏览器选项卡并导航到IdentityServer根URL来确认,并且可以肯定的是,登录的用户名按预期显示在右上角。

如上面链接的我的问题中所述,然后将用户定向到一个页面,从该页面需要选择希望处理的租户,然后再重定向回“客户端”应用程序(交互有效)。

但是,在选择了一个租户之后,TenantController然后调用HttpContext.SignInAsync([the currently logged-in user's subject claim value], [the selected tenant claim]),目的是使该租户重新登录已登录的用户,并传递附加的租户声明。 (这是我尝试使选定的TenantId声明出现在发送回客户端的令牌中……而且我乐于接受有关执行此操作的更好方法的建议。)

尽管此交互的完成确实确实将预期的用户信息传递回了发送给客户端的令牌中,但它实际上使用户退出了IdentityServer?!?在另一个选项卡中刷新身份服务器根URL表示没有用户登录。

这是为什么?我究竟做错了什么?选择租户之前,我需要用户保持登录身份服务器的身份。

1 个答案:

答案 0 :(得分:0)

我将HttpContext.SignInAsync的IdentityServer4提供的扩展替换为标准的内置(对Microsoft.AspNetCore.Authentication)HttpContext.SignInAsync

所以代替:

public static async Task SignInAsync(this HttpContext context, string subject, params Claim[] claims)

我使用的是IdentitySever4

public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties)

来自Microsoft.AspNetCore.Authentication。

为了调用它,我按如下方式建立了Principal:

var userId = User.Claims.Single(r => r.Type == "sub").Value;
var user = await _userManager.FindByIdAsync(userId);
var principal = await _claimsFactory.CreateAsync(user);
((ClaimsIdentity)principal.Identity).AddClaim(new Claim("TenantId", tenant.Id.ToString()));