Asp.net 核心,外部登录,没有配置身份验证处理程序来处理方案:Facebook



InvalidOperationException:没有配置身份验证处理程序来处理方案:Facebook

[HttpGet]
[AllowAnonymous]
public IActionResult ExternalLogin(string provider, string returnUrl = "CampAccount")
{
// Request a redirect to the external login provider.
var redirectUrl = Url.Action(nameof(ExternalLoginCallback), "Account", new { ReturnUrl = returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return Challenge(properties, provider);//Error occurs here
}

如果有人遇到同样的问题,我真的很感谢你的想法或关于我做错了什么的建议。

启动.cs

public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets<Startup>();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<CampionzDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// if using IdentityServer4
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Adds a default in-memory implementation of IDistributedCache.
services.AddDistributedMemoryCache();
services.AddSession();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
ExpireTimeSpan = TimeSpan.FromMinutes(60)
});
app.UseStaticFiles();
app.UseIdentity();
// Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

//External login Secrets
app.UseFacebookAuthentication(new FacebookOptions
{
AppId = "###",// Configuration["Authentication:Facebook:ClientID"],
AppSecret = "###",//Configuration["Authentication:Facebook:ClientSecret"],
});
app.UseGoogleAuthentication(new GoogleOptions
{
ClientId = "###",// Configuration["Authentication:Facebook:ClientID"],
ClientSecret = "###",//Configuration["Authentication:Facebook:ClientSecret"],
});
}
}

尝试在UseIdentity和UseMvc之间放置外部身份验证中间件

app.UseIdentity();

之后放置外部中间件

app.UseFacebookAuthentication(new FacebookOptions
{
AppId = "###",// Configuration["Authentication:Facebook:ClientID"],
AppSecret = "###",//Configuration["Authentication:Facebook:ClientSecret"],
});
app.UseGoogleAuthentication(new GoogleOptions
{
ClientId = "###",// Configuration["Authentication:Facebook:ClientID"],
ClientSecret = "###",//Configuration["Authentication:Facebook:ClientSecret"],
});

最后一个块应该是

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

是的,模式确实很重要 lot.so 遵循上面帖子和下面提到的正确模式。

  1. app.UseIdentity();
  2. app.UseFacebookAuthentication(new FacebookOptions...);// Others Authenticater also will come here like Google,LinkedIn etc also will come here
  3. app.UseMvc(routes =>{...});

最新更新