black_tears
black_tears

Reputation: 95

Github OAuth provider with ASP.NET Core 2.0 does not work

I've tried to setup Github as an external provider in ASP.NET Core 2.0, as followed:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie()
            .AddOAuth("Github", "Git Hub", gitHubOptions =>
            {
                gitHubOptions.ClientId = Configuration["Auth:Github:ClientId"];
                gitHubOptions.ClientSecret = Configuration["Auth:Github:ClientSecret"];
                gitHubOptions.CallbackPath = new PathString("/signin-github");
                gitHubOptions.AuthorizationEndpoint = "http://github.com/login/oauth/authorize";
                gitHubOptions.TokenEndpoint = "https://github.com/login/oauth/access_token";
            })

I have also setup a Github APP with a redirect url

enter image description here

The externel provider (Github) button is shown on the login page. When the button is pressed, the Github login page is also shown. After enter the credentials and press authorize, the user is redirected to the login page of my service, but a registration is not possible.

enter image description here

The same scenario works fine with Microsoft, Google and Facebook. An email-address is shown at the "register" page and the user could be registered.

Do you have any idea?

Upvotes: 2

Views: 1210

Answers (1)

Tratcher
Tratcher

Reputation: 6084

For the curious, the missing element here is mapping user information to claims. Here's an extract from the linked sample. https://github.com/aspnet/Security/blob/1367a5d3858d4446c126940fe5c26267d0ac2512/samples/SocialSample/Startup.cs#L175

o.ClientId = Configuration["github:clientid"];
o.ClientSecret = Configuration["github:clientsecret"];
o.CallbackPath = new PathString("/signin-github");
o.AuthorizationEndpoint = "https://github.com/login/oauth/authorize";
o.TokenEndpoint = "https://github.com/login/oauth/access_token";
o.UserInformationEndpoint = "https://api.github.com/user";
// Retrieving user information is unique to each provider.
o.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
o.ClaimActions.MapJsonKey(ClaimTypes.Name, "login");
o.ClaimActions.MapJsonKey("urn:github:name", "name");
o.ClaimActions.MapJsonKey(ClaimTypes.Email, "email", ClaimValueTypes.Email);
o.ClaimActions.MapJsonKey("urn:github:url", "url");

Upvotes: 5

Related Questions