我做我自己的系统进行身份验证在某些情况下JWT令牌。
当我正确验证令牌,我有
var userIdentity = await user.CreateIdentityAsync(DefaultAuthenticationTypes.ExternalBearer);
owinContext.Authentication.User = new System.Security.Claims.ClaimsPrincipal(userIdentity);
owinContext.Authentication.SignIn(userIdentity);
System.Web.HttpContext.Current.User = owinContext.Authentication.User;
await next()
但是这似乎并没有解决身份验证仍然无法在 - 我相信 - 在Asp.Net的mvc水平。因为我知道它使用HttpContext
我尝试调用next()
之前加入这个
HttpContext.Current.User = new GenericPrincipal(userIdentity, new string[0]);
这让我走得更远,但我似乎仍然得到一个授权错误,它似乎(通过搜索源,我得到的消息,并where its used)要从网络API [Authorize]
属性来。
只要通过.NET源代码跟踪我打墙。我应该得到这个消息的唯一途径是,如果IsAuthorized返回false。但是没有指定角色,也没有用户(这只是普通[Authorize]
),并在前往该next()
之前,我可以停止调试器,检查是有一个用户的身份,并且是它IsAuthorized
。
我已经覆盖了AuthorizeAttribute
,以设置断点,可以看到,当时它被称为但是,我actionContext
是与IsAuthorized == false
一个完全不同的身份相关联。这又使我不知道我是否在用户身份错我签约
所以......我在做正确吗?我应该怎么做?
我从来没有明白为什么,但在我的情况,我已经在登录后需要有效门票:
var userIdentity = await user.CreateIdentityAsync(DefaultAuthenticationTypes.ExternalBearer);
ctx.Authentication.SignIn(userIdentity);
AuthenticationTicket ticket = new AuthenticationTicket(userIdentity, null);
ctx.Validated(ticket);
编辑
我不是真的在同一个环境。就我而言,我有一个自定义身份验证提供Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationProvider
的继承:
public class CustomBearerAuthenticationProvider:OAuthBearerAuthenticationProvider
{
public CustomBearerAuthenticationProvider() : base()
{
this.OnValidateIdentity = (context) => Task.Run(() =>
{
var identity = this.CreateApplicationIdentity(user);
context.OwinContext.Authentication.SignIn(identity);
AuthenticationTicket ticket = new AuthenticationTicket(identity, null);
context.Validated(ticket);
});
}
}
context
的类型为:Microsoft.Owin.Security.OAuth.OAuthValidateIdentityContext