回答
令牌只是持有索賠,它只是用來驗證到資源。如果其中一項索賠持有用戶信息,您可以創建一個身份並向其分配索賠。
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. 保護從Azure獲取的持票人令牌AD
- 5. Owin身份令牌身份驗證令牌端點響應404
- 6. SessionId /身份驗證令牌生成的最佳做法
- 7. 如何使用JWT for Google Firebase生成身份驗證令牌?
- 8. 摘要式身份驗證生成'令牌'
- 9. 身份2.0 Web API爲客戶端生成令牌
- 10. 在GoLang中生成CosmosDB身份驗證令牌
- 11. 身份驗證令牌
- 12. CherryPy身份驗證令牌
- 13. Facebook身份驗證令牌
- 14. Angular2獲取持票人令牌網絡api
- 15. Feign Oauth如何手動設置持票人令牌值?
- 16. 生成LTPA令牌?
- 17. Flask令牌生成
- 18. CSRF令牌生成
- 19. 如何在使用asp.net web api的持票人令牌之後從令牌獲取用戶信息
- 20. 使用Silverlight隔離存儲保持身份驗證令牌?
- 21. 將SQL令牌生成器更改爲Java令牌生成器
- 22. Facebook身份驗證令牌未能創建Firebase身份驗證
- 23. 如何把ASP.net身份角色放入Identityserver4身份令牌
- 24. ADAL身份驗證令牌不同的郵遞員令牌Oauth2
- 25. 保護基於令牌的身份驗證系統的令牌
- 26. Hibernate中的身份生成器是單身人士嗎?
- 27. 制定重置身份驗證令牌
- 28. 無效Accountmanager身份驗證令牌
- 29. AWS缺失身份驗證令牌
- 30. 管理Facebook身份驗證令牌
你能解釋一下嗎?你如何使用持票人令牌?通過簡單的帖子或獲取方法?你想通過該令牌授權來電者嗎? –
@SamFarajpourGhamari我有一個標籤,所以它將是一個GET端點,我想授權用戶是他們要求的人 – Aziz