Authentication ASP.Net核心身份验证提供程序

Authentication ASP.Net核心身份验证提供程序,authentication,asp.net-core-mvc,google-oauth,asp.net-core-2.0,Authentication,Asp.net Core Mvc,Google Oauth,Asp.net Core 2.0,我使用身份验证提供程序创建了ASP.Net Core 2.0 MVC,如下所述: 在localhost上(即通过Visual Studio 2017运行时),所有功能都运行良好。然而,在部署到Azure之后,我发现登录提供程序停止工作(尽管我设置了适当的回调URI;例如,对于Google,我有https://localhost:44357/signin-谷歌但也https://mysite.azurewebsites.net/signin-google,https://example.com/s

我使用身份验证提供程序创建了ASP.Net Core 2.0 MVC,如下所述:

在localhost上(即通过Visual Studio 2017运行时),所有功能都运行良好。然而,在部署到Azure之后,我发现登录提供程序停止工作(尽管我设置了适当的回调URI;例如,对于Google,我有
https://localhost:44357/signin-谷歌
但也
https://mysite.azurewebsites.net/signin-google
https://example.com/signin-google
https://www.example.com/signin-google
(以及在Azure中设置
example.com
域及其
www
子域,并配置覆盖这些域的SSL)。对于Twitter,我已将设置更改为仅限www子域(因为只允许1个回调URL),对于LinkedIn,我仅拥有域和子域(也就是说,我必须删除localhost;因为LinkedIn只允许在单个域下使用回调URI)。我还配置了Azure应用程序服务的
应用程序设置下的
secrets.json
中的那些键/值

症状 首次登录时(又名注册),用户单击相关提供商的按钮,然后新的用户条目出现在
AspNetUsers
AspNetUserLogins
表中,用户被引导到可以关联其电子邮件的页面。但是,此时他们没有登录,只是注册了。随后的尝试会将他们带回电子邮件注册表istration表单;仅单击“注册”
按钮,然后返回一条错误消息,说明电子邮件已注册(这是正确的);但用户仍未登录到该站点

我对所有供应商都有同样的问题;不过在证明了这一点之后,我的大部分精力都集中在谷歌上,只是为了限制变化变量的数量

我对该示例所做的唯一重大更改是重构
Startup.cs
中的代码,以便每个提供程序都封装在自己的方法中;因此
ConfigureServices
包含:

ConfigureServicesAuthFacebook(services);
ConfigureServicesAuthGoogle(services);
ConfigureServicesAuthTwitter(services);
ConfigureServicesAuthMicrosoft(services);
ConfigureServicesAuthLinkedIn(services);
…这些方法如下所示:

#region Authentication Providers
public void ConfigureServicesAuthFacebook(IServiceCollection services)
{
    services.AddAuthentication().AddFacebook(x =>
    {
        x.AppId = Configuration["Authentication:Facebook:Id"];
        x.AppSecret = Configuration["Authentication:Facebook:Secret"];
    });
}
public void ConfigureServicesAuthGoogle(IServiceCollection services)
{
    services.AddAuthentication().AddGoogle(x =>
    {
        x.ClientId = Configuration["Authentication:Google:Id"];
        x.ClientSecret = Configuration["Authentication:Google:Secret"];
    });
}
public void ConfigureServicesAuthTwitter(IServiceCollection services)
{
    services.AddAuthentication().AddTwitter(x =>
    {
        x.ConsumerKey = Configuration["Authentication:Twitter:Id"];
        x.ConsumerSecret = Configuration["Authentication:Twitter:Secret"];
    });
}
public void ConfigureServicesAuthMicrosoft(IServiceCollection services)
{
    services.AddAuthentication().AddMicrosoftAccount(x =>
    {
        x.ClientId = Configuration["Authentication:Microsoft:Id"];
        x.ClientSecret = Configuration["Authentication:Microsoft:Secret"];
    });
}
public void ConfigureServicesAuthLinkedIn(IServiceCollection services)
{
    services.AddAuthentication().AddOAuth("LinkedIn", x =>
    {
        x.ClientId = Configuration["Authentication:LinkedIn:Id"];
        x.ClientSecret = Configuration["Authentication:LinkedIn:Secret"];
        x.CallbackPath = new PathString("/signin-linkedin"); 
        x.AuthorizationEndpoint = "https://www.linkedin.com/oauth/v2/authorization";
        x.TokenEndpoint = "https://www.linkedin.com/oauth/v2/accessToken";
        x.UserInformationEndpoint = "https://api.linkedin.com/v1/people/~:(id,formatted-name,email-address,picture-url)";
        //x.Scope = { "r_basicprofile", "r_emailaddress" };
    });
}
#endregion Authentication Providers
问题:
如果我无法在localhost上重新创建此问题,我如何调试此问题。有关此问题的任何提示?

其工作方式是,您的用户首先必须将其Google帐户与您系统上的用户关联。听起来这对您有效

完成后,您的代码应该执行某种类型的
externalloginginasync
,但是这种类型取决于您的系统设置方式

开箱即用,其中
不允许
这意味着与需要确认的帐户相关联的电子邮件或电话号码尚未确认。请参阅

查看
AccountController
方法
ExternalLoginConfirmation
,您将看到:

var user = new ApplicationUser(model.Email) { Email = model.Email };
假设您对那些与现有登录提供商注册的用户感到满意,请将其修改为:

var user = new ApplicationUser(model.Email) { Email = model.Email, EmailConfirmed = true };

我认为这取决于你如何在你的网站上设置注册页面。你要做的第一件事就是将帐户与你系统上的用户帐户相关联,然后你需要进行实际的外部登录gninasyncthanks@DalmTo;根据你的提示,我添加了一些代码来调查该方法返回了什么,并发现了什么在
result.IsNotAllowed
被设置为
true
;但是没有逻辑来处理该场景。现在正在调查原因;但这让我再次行动起来;谢谢。