在当前HttpContext会话中设置新的ACCESS_TOKEN

Setting new access_token in current HttpContext session(在当前HttpContext会话中设置新的ACCESS_TOKEN)
本文介绍了在当前HttpContext会话中设置新的ACCESS_TOKEN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在使用Identity Server 4实现令牌提供程序,并且希望让我的客户端应用程序向用户刷新令牌。

使用以下代码,我可以成功请求一对新的access_Tokenfresh_Token,并正确调用接口

public async Task<IActionResult> CallApiUsingUserRefreshToken()
{
   var oldAccessToken = await HttpContext.GetTokenAsync("access_token");
   var oldRefreshToken = await HttpContext.GetTokenAsync("refresh_token");

   var tokenClient = new TokenClient("http://localhost:5000/connect/token", "mvc", "secret");
   var newToken = await tokenClient.RequestRefreshTokenAsync(oldRefreshToken);

   var client = new HttpClient();
   client.SetBearerToken(newToken.AccessToken);
   var content = await client.GetStringAsync("http://localhost:5001/identity");

   ViewBag.Json = JArray.Parse(content).ToString();
   return View("json");
}
现在的问题是,如何在我的会话cookie中存储这个新对? 因此,下次我使用此方法时,我会使用await HttpContext.GetTokenAsync("access_token")我会得到最新的令牌


示例

第一个请求CallApiUsingUserRefreshToken()

旧的ACCESS_TOKEN eyJhbGciOiJSUzI1NiIsImtpZCI6IjY5YTA1ZDE5NTZiNjM1YTI5OTRkN2Q4MmUzOTVlZDNlIiwidHlwIjoiSldUIn0.eyJuYmYiOjE1MTAxNTA4ODIsImV4cCI6MTUxMDE1MDkxMiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9yZXNvdXJjZXMiLCJhcGkxIl0sImNsaWVudF9pZCI6Im12YyIsInN1YiI6IjY2MzQyOWUzLTIwNTYtNDg1OC1iN2RhLThmYjhiMDI4YTEyZiIsImF1dGhfdGltZSI6MTUxMDE1MDg4MCwiaWRwIjoibG9jYWwiLCJzY29wZSI6WyJvcGVuaWQiLCJwcm9maWxlIiw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pSIKVUUh4B164jkpUY0NiJefDAHogVTcSBsRbeJj6m6oiBGzKxx5JCCBB2g

旧的REFRESH_TOKEN 981eb24796cf0bf191f0845cb82a708420683de3bc5f7e4c07859287587bc43c

新建ACCESS_TOKEN eyJhbGciOiJSUzI1NiIsImtpZCI6IjY5YTA1ZDE5NTZiNjM1YTI5OTRkN2Q4MmUzOTVlZDNlIiwidHlwIjoiSldUIn0.eyJuYmYiOjE1MTAxNTMwMTQsImV4cCI6MTUxMDE1MzA0NCwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9yZXNvdXJjZXMiLCJhcGkxIl0sImNsaWVudF9pZCI6Im12YyIsInN1YiI6IjY2MzQyOWUzLTIwNTYtNDg1OC1iN2RhLThmYjhiMDI4YTEyZiIsImF1dGhfdGltZSI6MTUxMDE1MDg4MCwiaWRwIjoibG9jYWwiLCJzY29wZSI6WyJvcGVuaWQiLCJwcm9maWxlIiwiYXBpMSIsIm9mZmxpbmVfYWNjZXNzIl0sImFtciI6WyJwd2QiXX0.AMV5DblN48MxvWm4qbMNcAgGYTYGb0Rz-LAMX2-jAGtQhkis8tauWfpugm0aU_tCbwn6ktUQN1UepBCLpVe9qAo1hC4E_YDrhqxEKCrJK3NhWEYloNMAxFUJVak2rRO81gjH7zHZ-HvdxZaSzG2CGpkKOZKbnCuPSkXbUr_dgAulKH7Ul2hunqH9dCbX-BE9X7-ZjO6cXm8tZHOdRJBm7NsEvVo1bbSaTx4uEX0HDEX01uQAiUx6fz2j4Exp5A9CdlQhkqH4h5hSP3D3XvtH9wpRpD-kWlMRMxSwXeJ7P0OQMn04WOM-sGFXQFfUHwKxvSuMxcOx1MzUJF5-1W-kfg

新RefRes_Token 9b0130de391db80b5b7499f18297b84af79a4f6ef423fdd85fb4e7f487611562

我正在考虑如何将第一个请求中的newaccess_TokenREFRESH_TOKEN设置为在第二个请求中使用。

推荐答案

最终解决了问题。

对于任何想要在.Net身份验证Cookie内更新其当前令牌的人,您应该使用Microsoft身份验证包中提供的TokenStore

var auth = await HttpContext.AuthenticateAsync("Cookies");
auth.Properties.StoreTokens(new List<AuthenticationToken>()
{
    new AuthenticationToken()
    {
        Name = OpenIdConnectParameterNames.AccessToken,
        Value = newToken.AccessToken
    },
    new AuthenticationToken()
    {
        Name = OpenIdConnectParameterNames.RefreshToken,
        Value = newToken.RefreshToken
    }
});

await HttpContext.SignInAsync(auth.Principal, auth.Properties);

在执行HttpContext.SignInAsync时,这将覆盖会话中的access_TokenRefRes_Token,这是针对.Net Core 2.0的

这篇关于在当前HttpContext会话中设置新的ACCESS_TOKEN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)