如何在IdentityServer4中进行多步登录?
我们使用的是IdentityServer3,隐式授权,登录包含多个屏幕.在IdentityServer3中,内置支持这种多步登录工作流程(例如,用于接受EULA,双因素登录等),该功能称为“部分登录”,甚至还有一个示例:
https://github.com/IdentityServer/IdentityServer3.Samples/tree/master/source/CustomUserService/CustomUserService
我们最近升级到AspNetCore和IdentityServer4,并想知道如何实现相同的目标?也就是说,在第一步中检查用户名和密码,如果正确,则将其安全地存储(例如在加密的cookie中)以用于下一步骤. 解决方法
我们的解决方案是复制IdentityServer3的部分登录:使用自定义cookie在步骤之间保留数据.
首先,我们需要注册我们的自定义cookie身份验证(在Startup.Configure) app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "my-partial",AutomaticAuthenticate = false,AutomaticChallenge = false }); >登录工作流的第一步/入口点应映射到GET / account / login(从IdentityServer4 1.0.0-rc2开始). 码: var claims = new [] { new Claim("my-user",username),new Claim("some-attribute",someAttribute) }; await HttpContext.Authentication .SignInAsync("my-partial",new ClaimsPrincipal(new ClaimsIdentity(claims))); 重要提示:避免使用POST / account / login作为第二步.因为无论您的结果如何,IdentityServer的中间件都会将您重定向回授权端点(从RC2开始).只需选择任何其他路径. >在最后一步,关键部分 >我们从cookie中读取持久数据 在代码中 var partialUser = await HttpContext.Authentication.AuthenticateAsync("my-partial"); var username = partialUser?.Claims.FirstOrDefault(c => c.Type == "dr-user")?.Value; var claims = new [] { /* Your custom claims */}; await HttpContext.Authentication .SignOutAsync("my-partial"); await HttpContext.Authentication .SignInAsync(username,username,claims); return Redirect(returnUrl); 此外,您可能需要验证输入,例如,如果没有部分cookie,则返回第一步,等等. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Asp.net下利用Jquery Ajax实现用户注册检测(验证用户名是否
- asp.net-core – 如何在ASP.NET Core中实现angular 4 serve
- asp.net-mvc – 图像优化框架未初始化
- 我不能在ASP.NET MVC中做的事情
- 即使在完成所有可能的设置后,使用IIS 7在asp.net 2.0中超时
- asp.net – 你会认为这是一个单身/单身模式吗?
- .net – IAuthenticationRequest.RedirectToProvider不应该
- 如何在ASP.NET MVC中渲染HTML字符串?
- ASP.NET平台有相当于Heroku吗?
- asp.net-mvc – 在操作过滤器上使用缓存数据,以避免再次执行