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

bla*_*ars 2 c# github oauth-2.0 asp.net-core 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";
            })
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

在此输入图像描述

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

你有什么主意吗?

Tra*_*her 5

出于好奇,这里缺少的元素是将用户信息映射到声明。这是链接示例的摘录。 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");
Run Code Online (Sandbox Code Playgroud)