Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 547
user1336 Avatar asked Aug 11 '17 09:08

user1336


1 Answers

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);
like image 115
Kévin Chalet Avatar answered Oct 05 '22 22:10

Kévin Chalet