c# - 从 Web API 操作中的请求中检索 token 值

标签 c# .net rest asp.net-mvc-4 asp.net-identity

我有一个 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 持有者,然后找到 相应的用户。

所以:

  1. 我该怎么做?
  2. 最好的方法是什么?

最佳答案

您可以创建一个 DelegateHandlerActionFilter 来检查不记名 token 的请求 header 。

然后您可以检索用户,创建用户的适当 IPrincipal 并将其设置在当前线程和 HttpContext 中。

关于c# - 从 Web API 操作中的请求中检索 token 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37182442/

相关文章:

c# - 将多个 WSDL 版本反序列化为同一对象

c# - 删除窗口的标题栏文本但保留状态栏文本

javascript - 如何使用 Node.js 和 EXPRESS 在一个文件 Restful API 中设置两个根端点

java - 将我的自定义 http header 添加到 Spring RestTemplate 请求/扩展 RestTemplate

c# - silverlight 工具包引用问题

c# - 网络核心 : Swashbuckle Set operationId Automatically on to Controller Action Method

c# - 在使用 NHibernate 进行持久化的项目中测试数据模型

java - 使用 Apache Camel 发送 POST 请求

c# - .GetProperties() 的大 O

c# - C#中的分布式计算