回答
令牌只是持有索赔,它只是用来验证到资源。如果其中一项索赔持有用户信息,您可以创建一个身份并向其分配索赔。
public void ValidateBearerToken(OwinContext context)
{
try
{
var tokenHandler = new JwtSecurityTokenHandler();
byte[] securityKey = GetBytes("some key"); //this should come from a config file
SecurityToken securityToken;
var validationParameters = new TokenValidationParameters()
{
ValidAudience = "http://localhost:2000",
IssuerSigningToken = new BinarySecretSecurityToken(securityKey),
ValidIssuer = "Self"
};
var auth = context.Request.Headers["Authorization"];
if (!string.IsNullOrWhiteSpace(auth) && auth.Contains("Bearer"))
{
var token = auth.Split(' ')[1];
var principal = tokenHandler.ValidateToken(token, validationParameters, out securityToken);
context.Request.User = principal;
}
}
catch (Exception ex)
{
var message = ex.Message;
}
}
首先,您需要创建一些基于令牌的声明,然后创建ClaimsIdentity
并使用它来授权用户。
public ActionResoult Login(string token)
{
if(_tokenManager.IsValid(token))
{
// optionally you have own user manager which returns roles and user name from token
// no matter how you store users and roles
var user=_myUserManager.GetUserRoles(token);
// user is valid, going to authenticate user for my App
var ident = new ClaimsIdentity(
new[]
{
// adding following 2 claim just for supporting default antiforgery provider
new Claim(ClaimTypes.NameIdentifier, token),
new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),
// an optional claim you could omit this
new Claim(ClaimTypes.Name, user.Username),
// populate assigned user's role form your DB
// and add each one as a claim
new Claim(ClaimTypes.Role, user.Roles[0]),
new Claim(ClaimTypes.Role, user.Roles[1]),
// and so on
},
DefaultAuthenticationTypes.ApplicationCookie);
// Identity is sign in user based on claim don't matter
// how you generated it
HttpContext.GetOwinContext().Authentication.SignIn(
new AuthenticationProperties { IsPersistent = false }, ident);
// auth is succeed, just from a token
return RedirectToAction("MyAction");
}
// invalid user
ModelState.AddModelError("", "We could not authorize you :(");
return View();
}
现在你可以使用Authorize
过滤器,以及:
[Authorize]
public ActionResult Foo()
{
}
// since we injected user roles to Identity we could do this as well
[Authorize(Roles="admin")]
public ActionResult Foo()
{
// since we injected our authentication mechanism to Identity pipeline
// we have access current user principal by calling also
// HttpContext.User
}
而且我鼓励你有看Token Based Authentication Sample从我的github回购是一个非常简单的工作示例。
没有_myUserManager.GetUserRoles(token); ...方法。我正在使用公共类SimpleAuthorizationServerProvider:OAuthAuthorizationServerProvider - 这有帮助吗? – Aziz
'_myUserManager'就是一个例子。展示如何使用自己的类来生成声明并基于用户登录。在这个例子中,我使用'_myUserManager'来提取当前用户角色,你可以实现你自己的类。用户名和角色只是简单字符串。如果您没有用户名或角色,可选。如果你看看我的github回购,你可以看到我只用一个字符串来授权用户。 –
我没有当前的用户上下文。我需要从字符串令牌转到用户。我需要知道_myUserManager.GetUserRoles(token)里面的locic; – Aziz
- 1. ASP.NET身份 - 生成令牌
- 2. 如何将角色添加到asp身份持票人令牌
- 3. asp.net身份过期20分钟后持票人令牌
- 4. 从持票人令牌检索信息
- 5. 从持票人令牌web api获取用户数据
- 6. 保护从Azure获取的持票人令牌AD
- 7. 使用OAuth和持票人令牌将用户角色从ASP.NET身份返回到AngularJS应用程序
- 8. Owin身份令牌身份验证令牌端点响应404
- 9. 如何使用JWT for Google Firebase生成身份验证令牌?
- 10. 身份验证令牌的Python只生成字符串文字
- 11. SessionId /身份验证令牌生成的最佳做法
- 12. 身份2.0 Web API为客户端生成令牌
- 13. 摘要式身份验证生成'令牌'
- 14. 在GoLang中生成CosmosDB身份验证令牌
- 15. 撤销在ASP.NET身份2.0中由UserTokenProvider生成的令牌
- 16. CherryPy身份验证令牌
- 17. Facebook身份验证令牌
- 18. 身份验证令牌
- 19. 与身份验证令牌
- 20. 基于令牌的身份验证票据部分
- 21. 令牌生成
- 22. Angular2获取持票人令牌网络api
- 23. Google持票人令牌:哪一个是正确的?
- 24. SPA,Web API持票人令牌安全问题
- 25. Feign Oauth如何手动设置持票人令牌值?
- 26. 生成LTPA令牌?
- 27. CSRF令牌生成
- 28. Flask令牌生成
- 29. 如何使用Safari作为客户端从WebAPI获取JWT持票人令牌?
- 30. 如何在使用asp.net web api的持票人令牌之后从令牌获取用户信息
你能解释一下吗?你如何使用持票人令牌?通过简单的帖子或获取方法?你想通过该令牌授权来电者吗? –
@SamFarajpourGhamari我有一个标签,所以它将是一个GET端点,我想授权用户是他们要求的人 – Aziz