【问题标题】:Github OAuth provider with ASP.NET Core 2.0 does not work带有 ASP.NET Core 2.0 的 Github OAuth 提供程序不起作用
【发布时间】:2018-02-28 18:47:18
【问题描述】:

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

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

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

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

同样的场景适用于 Microsoft、Google 和 Facebook。在“注册”页面上会显示一个电子邮件地址,并且可以注册用户。

你有什么想法吗?

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-02-09
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多