首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Asp.net核心,外部登录,没有配置身份验证处理程序来处理该方案:

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

Stack Overflow用户
提问于 2017-07-19 17:44:35
回答 2查看 649关注 0票数 1

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

代码语言:javascript
运行
复制
[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
    }

如果有人遇到同样的问题,我会非常感谢你的想法和建议,我做错了什么,我正在使用(IdentityServer4)。

Startup.cs

代码语言:javascript
运行
复制
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"],
        });
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-20 08:52:37

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

代码语言:javascript
运行
复制
app.UseIdentity();

在那之后放外卖的中间人

代码语言:javascript
运行
复制
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"],
    });

最后一个街区应该是

代码语言:javascript
运行
复制
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
票数 1
EN

Stack Overflow用户

发布于 2017-07-20 16:37:42

是的,模式确实很重要,alot.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 =>{...});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45197574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档