Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从HttpresponseMessage Asp.net Web Api返回承载令牌_C#_Asp.net Mvc_Asp.net Web Api2 - Fatal编程技术网

C# 如何从HttpresponseMessage Asp.net Web Api返回承载令牌

C# 如何从HttpresponseMessage Asp.net Web Api返回承载令牌,c#,asp.net-mvc,asp.net-web-api2,C#,Asp.net Mvc,Asp.net Web Api2,如何从web api返回令牌,以便在javascript中设置该令牌的cookie? 通过这种方式,我正在生成令牌 [Route("api/agency/login")] [HttpPost] public HttpResponseMessage loginApi([FromBody] Users UserObj) { var tokanObj = method.AccessToken(UserObj.username, UserObj.Pas

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

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

您也可以考虑将令牌作为响应

中的cookie。
[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;
}

您使用的web api版本是什么(.net core或web api 2带完整的.net framework)?Asp.net web api 2将HttpResponseMessage更改为String(或令牌对象的任何类型),并直接返回tokenObj什么是tokenObj类型?对象类型是VAR您使用的web api版本是什么(.net core或web api 2带完整的.net framework)?Asp.net web API2将HttpResponseMessage更改为字符串(或令牌对象的任何类型),并直接返回tokenObj什么是tokenObj类型?对象类型为VAR但我可以使用sessionstorage在javascript中将此令牌设置为cookie吗?当然可以。看看这个例子,但是我可以使用sessionstorage在javascript中将这个令牌设置为cookie吗?当然可以。看看这个例子