TAHA SULTAN TEMURI
TAHA SULTAN TEMURI

Reputation: 5161

How to return bearer token from HttpresponseMessage Asp.net Web Api

How can I return token from web api so that I can set cookie of that token in javascript? In this way I am generating token

   [Route("api/agency/login")]
    [HttpPost]
    public HttpResponseMessage loginApi([FromBody] Users UserObj)
    {
           var tokanObj = method.AccessToken(UserObj.username, UserObj.Password, "password");

   //How to return this token ??
    }

Upvotes: 1

Views: 2685

Answers (2)

Nkosi
Nkosi

Reputation: 246998

You can also consider including the token as a cookie in the response

[Route("api/agency/login")]
[HttpPost]
public HttpResponseMessage loginApi([FromBody] Users UserObj) {
    var tokanObj = method.AccessToken(UserObj.username, UserObj.Password, "password");

    var response = Request.CreateResponse(HttpStatusCode.OK);
    //use what every you want as the key for the cookie
    var cookie = new CookieHeaderValue("auth_token_key", tokenObj);

    //...set cookie values as needed

    response.Headers.AddCookies(new[] { cookie });

    return response;
}

Upvotes: 2

Murat Gündeş
Murat Gündeş

Reputation: 859

Did you try StringContent ?

return new HttpResponseMessage( HttpStatusCode.OK ) 
{
    Content =  new StringContent( "Your token here" ) 
};

Upvotes: 2

Related Questions