How to set header using IHttpActionResult

2019-07-28 21:01发布

问题:

i want to add token to header for webapi request. how can i do this ?

this is my sample code

public IHttpActionResult Authenticate(Login data)
            {
                var Token = "fdsf123546fskjhf-gsuyuhsh";

                //here add these token to header

                return ?
            }

thank you.

回答1:

  1. You could use ResponseMessageResult class for that:

    public IHttpActionResult Authenticate(Login data)
    {
        var Token = "fdsf123546fskjhf-gsuyuhsh";
    
        var response = new HttpResponseMessage();
    
        response.Headers.Add("Authentication", Token);
    
        return new ResponseMessageResult(response);
    }
    
  2. Or create your own result class:

    public class HeaderActionResult : IHttpActionResult
    {
        private Tuple<string, string> header;
    
        public HeaderActionResult(Tuple<string, string> header) 
        {
            this.header = header;
        }
    
        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) 
        {
            var response = new HttpResponseMessage();
    
            response.Headers.Add(header.Item1, header.Item2);
    
            return Task.FromResult<HttpResponseMessage>(response);
        }
    }
    
    public IHttpActionResult Authenticate(Login data)
    {
        var Token = "fdsf123546fskjhf-gsuyuhsh";
    
        return new HeaderActionResult(Tuple.Create("Authentication", Token));
    }
    
  3. Or add a header to any existing IHttpActionResult object using an extension method:

    public static class HttpActionResultExtensions
    {
        public static IHttpActionResult AddHeader(this IHttpActionResult actionResult, string name, string value)
        {
            return new HeaderActionResult(actionResult, name, value);
        }
    
        private class HeaderActionResult : IHttpActionResult
        {
            private readonly IHttpActionResult actionResult;
    
            private string name;
    
            private string value;
    
            public HeaderActionResult(IHttpActionResult actionResult, string name, string value)
            {
                this.actionResult = actionResult;
    
                this.name = name;
    
                this.value = value;
            }
    
            public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
            {
                HttpResponseMessage httpResponseMessage = await this.actionResult.ExecuteAsync(cancellationToken);
    
                httpResponseMessage.Headers.Add(this.name, this.value);
    
                return httpResponseMessage;
            }
        }
    }
    


回答2:

This code working me

    public IHttpActionResult Authenticate(Login data)
            {
                        Status = "success";
                        Message = "You have been Authenticated successfully";
                        var Token = "fdsf123546fskjhf-gsuyuhsh";
                        var Result = new { Status = Status, Message = Message };

                        var Response = Request.CreateResponse(HttpStatusCode.OK, Result);
                        Response.Headers.Add("AccessToken", Token.AccessToken);

                        return ResponseMessage(Response);
            }