如何从HttpresponseMessage Asp.net Web Api返回承载令牌 [英] How to return bearer token from HttpresponseMessage Asp.net Web Api

查看:91
本文介绍了如何从HttpresponseMessage Asp.net Web Api返回承载令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Web api返回令牌,以便可以在javascript中设置该令牌的cookie? 通过这种方式,我生成了令牌

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

推荐答案

您还可以考虑将令牌作为cookie包含在响应中

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

这篇关于如何从HttpresponseMessage Asp.net Web Api返回承载令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆