我有一个允许匿名用户的ASP.NET Core MVC应用程序。这个程序是调用由身份服务器4.我已经创建了Identity Server的客户端描述MVC应用程序(客户端)的保护,给予其访问API范围这样的ASP.NET Web API:使用客户端证书持久化令牌的最佳实践流程
new Client
{
ClientId = "my-mvc-client-app",
AllowedGrantTypes = GrantTypes.ClientCredentials,
RequireConsent = false,
ClientSecrets = new List<Secret> { new Secret("this-is-my-secret".Sha256()) },
AllowedScopes = new List<string>
{
StandardScopes.OpenId.Name,
StandardScopes.Profile.Name,
StandardScopes.OfflineAccess.Name,
"my-protected-api"
},
RedirectUris = new List<string>
{
"http://localhost:5009/signin-oidc",
}
}
在我的MVC应用程序,我使用TokenClient
获得令牌发出请求,以这样的受保护的API时,我可以使用:
var disco = await DiscoveryClient.GetAsync("http://localhost:5010");
var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, clientSecret);
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("hrmts-test-candidate-api-scope");
这工作得很好,但我请求来自Identity Server的新令牌在每个请求上,这可能不是一个好主意。
处理令牌的最佳做法是什么?我如何将它们保存在客户端(MVC应用程序)上,以及如何处理刷新令牌以确保客户端在必要时获取新令牌?
我喜欢这种方法,但是,如果这是作为一个单身人士的范围是这个实现线程安全和可重入安全吗? – LugTread