C# AspNetCore.Identity中的Google身份验证
开始使用AspNetCore.Identity,但无法运行简单示例,始终会收到以下异常: 处理请求时发生未处理的异常。 InvalidOperationException:未将身份验证处理程序配置为 处理方案:谷歌 Startup.csC# AspNetCore.Identity中的Google身份验证,c#,asp.net-core,asp.net-identity,asp.net-core-mvc,.net-core,C#,Asp.net Core,Asp.net Identity,Asp.net Core Mvc,.net Core,开始使用AspNetCore.Identity,但无法运行简单示例,始终会收到以下异常: 处理请求时发生未处理的异常。 InvalidOperationException:未将身份验证处理程序配置为 处理方案:谷歌 Startup.cs public void ConfigureServices(IServiceCollection services) { // EF services services.AddEntityFramework()
public void ConfigureServices(IServiceCollection services)
{
// EF services
services.AddEntityFramework()
.AddEntityFrameworkSqlServer()
.AddDbContext<MyContext>();
// Identity services
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<MyContext>()
.AddDefaultTokenProviders();
// MVC services
services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.Converters = new JsonConverter[] { new StringEnumConverter(), new IsoDateTimeConverter() };
});
AuthController.cs
[HttpGet]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
var redirectUrl = Url.Action("ExternalLoginCallback", "Auth", new { ReturnUrl = returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return new ChallengeResult(provider, properties);
}
在返回ChallengeResult
后的某个地方发生异常。
我错过了什么吗?您正在使用
app.UseIdentity()
并将google中间件上的AutomaticAuthenticate=true
设置为true。Identity将cookie auth设置为AutomaticAuthenticate,并且您只能将单个身份验证中间件设置为automatic,否则行为未定义
您将在中看到,当连接facebook时,它未设置为自动验证。我尝试了您的代码,并成功地将其重定向到google(
ExternalLogin
未被调用,因为AutomaticChallenge=true
)。看来还有另一个问题。我怀疑谷歌认证后可能会出现异常。我会将其标记为正确答案,谢谢。但我的问题是,在所有你的建议之后,我通过了“谷歌”提供商,而不是[G]oogle(大写),在我改变它之后,它开始工作:/
[HttpGet]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
var redirectUrl = Url.Action("ExternalLoginCallback", "Auth", new { ReturnUrl = returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return new ChallengeResult(provider, properties);
}