M
M
Mikhail2019-03-02 00:55:54
OAuth
Mikhail, 2019-03-02 00:55:54

Why doesn't authorization through github work?

Hello. I'm trying to manually register on the site through social networks (ASP.NET Core 2.0) without using third-party frameworks. I settled on github. It seems that everything was done according to the examples, but for some reason it does not work. So the code in Startup.cs

services.AddOAuth("GitHub", options => 
{
     options.ClientId = Configuration["Auth:GitHub:ClientId"];
     options.ClientSecret = Configuration["Auth:GitHub:ClientSecret"];
     //Account/githubLogin2
     options.CallbackPath = new PathString(Configuration["Auth:GitHub:CallbackPath"]);

     options.AuthorizationEndpoint = Configuration["Auth:GitHub:AuthorizationEndpoint"];
     options.TokenEndpoint = Configuration["Auth:GitHub:TokenEndpoint"];
     options.UserInformationEndpoint = Configuration["Auth:GitHub:UserInformationEndpoint"];

     options.SaveTokens = true;

     options.ClaimActions.Clear();
     options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
     options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
     options.ClaimActions.MapJsonKey("urn:github:login", "login");
     options.ClaimActions.MapJsonKey("urn:github:url", "html_url");
     options.ClaimActions.MapJsonKey("urn:github:avatar", "avatar_url");

     options.Events = new OAuthEvents
     {
         OnCreatingTicket = async context =>
         {
             var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
             request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
             request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);

             var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
             response.EnsureSuccessStatusCode();

             var user = JObject.Parse(await response.Content.ReadAsStringAsync());
             context.RunClaimActions(user);
         }
    };
});

All code from above completely fulfills without errors (checked by a debugger). All tokens match. But when I try to switch to http://localhost:56666/account/githubLogin2after successfully logging into the github, I see this in the pop-up window:
5c79afd146ae3720651680.png
What is the error?
PS I did everything according to these tutorials:
1. www.jerriepelser.com
2. Example from github

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question