我使用身份验证提供程序创建了一个 ASP.Net Core 2.0 MVC,如下所述:https : //docs.microsoft.com/en-us/aspnet/core/security/authentication/social/
在本地主机上(即通过 Visual Studio 2017 运行时)一切正常。但是,在部署到 Azure 后,我发现登录提供程序停止工作(尽管我设置了适当的回调 URI;例如,对于 Google,我https://localhost:44357/signin-google
还有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在单个域下)。我还配置了那些secrets.json
在 Azure 应用服务的Application Settings
.
首次登录(也称为注册)时,用户单击相关提供商的按钮,然后新用户条目出现在AspNetUsers
和AspNetUserLogins
表中,并且用户将被定向到可以关联其电子邮件的页面。但是,那时他们还没有登录;刚注册。随后的尝试将他们带回电子邮件注册表;仅单击该Register
按钮就会返回一条错误消息,指出该电子邮件已注册(这是正确的);但用户仍未登录该站点。
我对所有供应商都有同样的问题;尽管在证明这一点之后,我将大部分精力集中在 Google 上,只是为了限制变化变量的数量。
我从示例中所做的唯一重要更改是重构代码,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
鉴于我无法在本地主机上重新创建问题,我该如何调试此问题。关于问题可能是什么的任何提示?
它的工作方式是您的用户首先必须将他们的 Google 帐户与您系统上的用户相关联。听起来这对你有用。
完成后,您的代码应该执行某种形式,ExternalLoginSignInAsync
但是这种类型取决于您的系统设置方式。
开箱即用,其中的IsNotAllowed
是true
这种方式与需要确认尚未被证实的帐户相关的电子邮件或电话号码。请参阅ASN.NET Core 2.0 Facebook 身份验证 ExternalLoginSignInAsync Fails (IsNotAllowed)
看看这个AccountController
方法ExternalLoginConfirmation
,你会看到:
var user = new ApplicationUser(model.Email) { Email = model.Email };
假设您对与现有登录提供商签约的人感到满意,请将其修改为:
var user = new ApplicationUser(model.Email) { Email = model.Email, EmailConfirmed = true };
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句