简体   繁体   中英

Get AuthenticationInfo in ASP.NET Core 2.0

How do I get the AuthenticationInfo property from the HttpContext in ASP.NET Core 2.0. I understand that with the redesign of Security in ASP.NET Core 2.0 the AuthenticationManager is now obsolete and that I should remove .Authentication .

I used to do something like this in 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) was replaced by IAuthenticationService.AuthenticateAsync(string) in 2.0: it now returns an AuthenticateResult but it works exactly the same way.

Your snippet can be updated to:

var result = await httpContext.AuthenticateAsync();
result.Properties.StoreTokens(new List<AuthenticationToken>
{
    new AuthenticationToken
    {
        Name = OpenIdConnectParameterNames.AccessToken,
        Value = accessToken
    },
    new AuthenticationToken
    {
        Name = OpenIdConnectParameterNames.RefreshToken,
        Value = refreshToken
    }
});

await httpContext.SignInAsync(result.Principal, result.Properties);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM