Reputation: 5161
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
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
Reputation: 859
Did you try StringContent ?
return new HttpResponseMessage( HttpStatusCode.OK )
{
Content = new StringContent( "Your token here" )
};
Upvotes: 2