我有一个 web api Controller ,我在其中使用 token 持有者来验证连接的用户:
protected ApplicationUserManager UserManager
{
get
{
return _UserManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
}
[Route("Create")]
[HttpPost]
public IHttpActionResult CreateEvent(CreateEventModel model)
{
String userID = UserManager.FindByName(User.Identity.Name).Id;
int EventID = EventRep.CreateEvent(userID, model.EventTitle, model.EventDate, model.NbParticipent, model.Description, model.Logo);
if(EventID > 0) return Ok(EventID);
return BadRequest("paramètres invalides") ;
}
因为 restful 应用程序必须是无状态的,所以我想避免使用 User.Identity
并替换这一行
String userID = UserManager.FindByName(User.Identity.Name).Id;
我需要从请求中检索 token 持有者,然后找到 相应的用户。
所以:
- 我该怎么做?
- 最好的方法是什么?
最佳答案
您可以创建一个 DelegateHandler
或 ActionFilter
来检查不记名 token 的请求 header 。
然后您可以检索用户,创建用户的适当 IPrincipal
并将其设置在当前线程和 HttpContext
中。
关于c# - 从 Web API 操作中的请求中检索 token 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37182442/