如何从ASP.NET Core 2.0中的HttpContext获取AuthenticationInfo属性.我理解,随着ASP.NET Core 2.0中安全性的重新设计,AuthenticationManager现在已经过时,我应该删除.Authentication.
我曾经在1.1.2中做过类似的事情
- var info = await httpContext.Authentication.GetAuthenticateInfoAsync("Automatic");
- info.Properties.StoreTokens(new List<AuthenticationToken>
- {
- new AuthenticationToken
- {
- Name = OpenIdConnectParameterNames.AccessToken,Value = accessToken
- },new AuthenticationToken
- {
- Name = OpenIdConnectParameterNames.RefreshToken,Value = refreshToken
- }
- });
- await httpContext.Authentication.SignInAsync("Automatic",info.Principal,info.Properties);
解决方法
AuthenticationManager.GetAuthenticateInfoAsync(string)在2.0中被IAuthenticationService.AuthenticateAsync(string)取代:它现在返回AuthenticateResult,但它的工作方式完全相同.
您的代码段可以更新为:
- var result = await httpContext.AuthenticateAsync();
- result.Properties.StoreTokens(new List<AuthenticationToken>
- {
- new AuthenticationToken
- {
- Name = OpenIdConnectParameterNames.AccessToken,Value = refreshToken
- }
- });
- await httpContext.SignInAsync(result.Principal,result.Properties);