简体   繁体   中英

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

在此处输入图片说明

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.

在此处输入图片说明

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?

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");

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