Web Api和会话变量?

时间:2013-11-28 12:01:39

标签: c# asp.net-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...
    }
}

我正在获取请求中的用户ID,并且我在服务中设置了用户。但是,我有更多的控制器,而且还有更多的操作。

这是这样做的方法还是我可以为'session'设置userId的任何替代方法(虽然不使用Web API中的标准Session)?

3 个答案:

答案 0 :(得分:1)

您可以使用MessageHandler或ActionFilter实现这些交叉问题。

答案 1 :(得分: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来执行此操作,但使用ICustomerService分享Controller会更复杂。

答案 2 :(得分: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