c# – 在ASP.NET Core 2.0中获取AuthenticationInfo

前端之家收集整理的这篇文章主要介绍了c# – 在ASP.NET Core 2.0中获取AuthenticationInfo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何从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);

猜你在找的.NET Core相关文章