使用ASP.NET Core 2.0的Github OAuth提供程序不起作用

时间:2017-09-19 20:57:01

标签: c# github asp.net-core oauth-2.0 asp.net-core-2.0

我尝试将Github设置为ASP.NET Core 2.0中的外部提供程序,如下所示:

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

我还设置了一个带有重定向网址的Github APP

enter image description here

外部提供程序(Github)按钮显示在登录页面上。按下按钮时,也会显示Github登录页面。输入凭据并按授权后,用户将被重定向到我的服务的登录页面,但无法进行注册。

enter image description here

同样的情况适用于微软,谷歌和Facebook。电子邮件地址显示在“注册”页面,用户可以注册。

你有什么想法吗?

1 个答案:

答案 0 :(得分:2)

对于好奇,这里缺少的元素是将用户信息映射到声明。这是链接样本的摘录。 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");