简体   繁体   中英

How can I connect with an external authentication server with ASP.NET Core in C#

I'm trying to authenticate with ASP.NET Core using the Google API, to be able to access the people api.

I have the following code:

 services.AddAuthentication()
           .AddOpenIdConnect(GoogleDefaults.AuthenticationScheme,
               GoogleDefaults.DisplayName,
               options =>
               {
                   options.Authority = "https://accounts.google.com/o/oauth2/v2/auth";
                   options.ClientId = Configuration["Authentication:Google:ClientId"];
                   options.ClientSecret = Configuration["Authentication: Google:ClientSecret"];
                   options.ResponseType = OpenIdConnectResponseType.Code;
                   options.Scope.Add("openid");
                   options.Scope.Add("email");
                   options.CallbackPath = "/signin-google";
               });

The problem is that I get the following exception

An unhandled exception occurred while processing the request.
IOException: IDX20807: Unable to retrieve document from: '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'. HttpResponseMessage: '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]', HttpResponseMessage.Content: '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'

How can I solve this issue?

Second question: how can I access the access token from the controller in order to be able to send requests to the People API?

Best regards, Mike

[regarding how to access the user in the controller]

Do you use JWT token? if so, i access it with:

this.HttpContext.User;

then, if you haven't modify that yet, the jwt payload is divided in several claims. In my case, by default the token is stateful(meaning that carries user information in it) and the user id was stored in the fifth claim

.Claims.ToList()[5].Value;

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