2013-11-28 117 views
1

我有一个web api控制器,我需要审核做出更改的用户。目前我执行以下操作:Web Api和会话变量?

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... 
    } 
} 

我得到请求中的用户标识,并将用户设置在服务中。但是,我有更多的控制器,而且这些控制器有更多的操作。

这是做到这一点的方法,或者是否有其他方法可以为'会话'设置userId(虽然不使用Web API中的标准会话)?

+0

你可以存储'userId'一个'里面索赔'并使用'Identity'在所有动作中检索它。 –

回答

1

你可以做一个基本的控制器:

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到做这个但是会有更多的复杂性与Controller分享ICustomerService

1

您可以通过MessageHandler或ActionFilter实现这些横切关注点。

1

可以继承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... 
    } 
} 

如果您想更详细地看,看到这里

enter link description here

+0

我同时使用了BaseController和MessageHandler答案,现在它工作正常! –