我无法在我的控制器方法中进行身份验证,作为我的确认用户注册工作流程的一部分,我正在寻找一些指导我的实现是正确的 .
我的控制器方法有以下代码;我已经确认用户已填充(作为 FindById
调用的一部分)但是在 SignIn
之后;未设置 this.Authentication.User.Identity
( Name
为空, IsAuthenticated
为false):
this.Authentication.SignOut("JWT");
ApplicationUser user = await this.AppUserManager.FindByIdAsync(userId);
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(this.AppUserManager, "JWT");
this.Authentication.SignIn(new AuthenticationProperties() { IsPersistent = true }, oAuthIdentity);
这是使用OWIN OAuth时自动登录的正确方法吗?
完整的控制器方法可以在这里看到:https://gist.github.com/chrismoutray/8a8e5f6a7b433571613b
作为参考,我一直在关注一个名为Bit of Tech的博客中的一组文章,该博客允许我使用JWT bearer token设置OWIN OAuth .
第3部分特别谈到JWT(5):http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-and-identity-2/
从我创建的自定义OAuth提供程序继承自 OAuthAuthorizationServerProvider
并实现 GrantResourceOwnerCredentials
,当我的客户端代码(AngularJS)尝试使用 endpoints /oauth/token
进行身份验证时,我可以看到它提供了正确的响应和安全 endpoints (使用然后可以访问授权属性) .
因此,通过中间件进行身份验证确实有效,但是从控制器方法内部进行身份验证的正确方法是什么?
1 回答
对于它的 Value ,我想我会分享我提出的解决方案 .
请点击此处参考:https://gist.github.com/chrismoutray/159e6fd74f45d88efd12
摘要 - 在
AccountController ConfirmSignUp
方法中;我使用用户管理器生成一个我称为GRANT-ACCESS
的自定义令牌,然后使用uri中的用户名和令牌重定向到我的confirm-signup
页面 . 我的角度应用程序将ui路由解析为confirm-signup
并执行登录,将令牌作为密码传递 .最后有
GrantResourceOwnerCredentials
的修正,所以如果FindAsync
(通过用户名和密码)没有返回用户,那么我再试一次,但这次将context.Password
视为GRANT-ACCESS
用户令牌进行验证 . 如果令牌有效,那么我返回JWT身份验证票证,就像用户使用有效密码登录一样 .