Answer the question
In order to leave comments, you need to log in
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);
}
};
});
http://localhost:56666/account/githubLogin2
after successfully logging into the github, I see this in the pop-up window: Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question