C# Web Api和会话变量?
我有一个web api控制器,我需要审核进行更改的用户。目前我做了以下工作:C# Web Api和会话变量?,c#,asp.net-web-api,C#,Asp.net Web Api,我有一个web api控制器,我需要审核进行更改的用户。目前我做了以下工作: public class CustomerController : ApiController { private readonly ICustomerService customerService; private bool userSet = false; public CustomerController(ICustomerService customerService) {
public class CustomerController : ApiController
{
private readonly ICustomerService customerService;
private bool userSet = false;
public CustomerController(ICustomerService customerService)
{
this.customerService = customerService;
}
[NonAction]
private SetUser(string userId)
{
if (userSet)
return;
//Get user from repository... here just for the example
var User = GetUser(userId);
customerService.SetUser(user);
userSet = true;
}
public Customer GetCustomer()
{
CookieHeaderValue cookie = Request.Headers.GetCookies("userId").FirstOrDefault();
SetUser(cookie["userId"].Value);
//code...
}
public int PostCustomer(Customer customer)
{
CookieHeaderValue cookie = Request.Headers.GetCookies("userId").FirstOrDefault();
SetUser(cookie["userId"].Value);
//code...
}
public void PutCustomer(int id, Customer customer)
{
CookieHeaderValue cookie = Request.Headers.GetCookies("userId").FirstOrDefault();
SetUser(cookie["userId"].Value);
//code..
}
public void DeleteCustomer(int id)
{
CookieHeaderValue cookie = Request.Headers.GetCookies("userId").FirstOrDefault();
SetUser(cookie["userId"].Value);
//code...
}
}
我正在获取请求中的用户ID,并在服务中设置用户。然而,我有更多的控制器,这些控制器有更多的动作
这是实现这一点的方法,还是有其他方法可以为“会话”设置用户ID(尽管不使用Web API中的标准会话)?您可以通过MessageHandler或ActionFilter实现这些交叉关注点。您可以制作一个基本控制器:
public abstract AuditableActionController : ApiController
{
private readonly ICustomerService customerService;
protected AuditableActionController(ICustomerService customerService)
{
this.customerService = customerService;
}
protected ICustomerService CustomerService { get { return this.customerService; } }
protected override void Initialize(HttpControllerContext controllerContext)
{
SetUser(cookie["userId"].Value);
// Make sure you call the base method after!
base.Initialize(controllerContext);
}
private void SetUser(string userId) { ... }
}
然后简单地从它继承所有需要审计的控制器
public class CustomerController : AuditableActionController
您也可以使用
ActionFilter
s来执行此操作,但是与控制器共享icCustomerService
将更加复杂。您可以继承DelegatingHandler
,
例如:
public class MessageHandler1 : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
Debug.WriteLine("Process request");
// Call the inner handler.
var response = await base.SendAsync(request, cancellationToken);
Debug.WriteLine("Process response");
return response;
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MessageHandlers.Add(new MessageHandler1());
// Other code not shown...
}
}
public类MessageHandler1:DelegatingHandler
{
受保护的异步覆盖任务SendAsync(
HttpRequestMessage请求,CancellationToken CancellationToken)
{
Debug.WriteLine(“进程请求”);
//调用内部处理程序。
var response=await base.sendaync(请求、取消令牌);
Debug.WriteLine(“进程响应”);
返回响应;
}
}
公共静态类WebApiConfig
{
公共静态无效寄存器(HttpConfiguration配置)
{
config.MessageHandlers.Add(newmessagehandler1());
//其他代码未显示。。。
}
}
如果您想查看更多详细信息,请参见此处
您可以将用户ID
存储在声明中
并使用标识在所有操作中检索它。我使用了BaseController和MessageHandler应答,现在工作正常!!