InvalidOperationException:没有配置IAuthenticationSignInHandler来处理该方案的登录:Identity.Application

时间:2018-01-18 11:02:28

标签: c# asp.net-core asp.net-core-mvc asp.net-identity

您好。我创建了新的AspNet核心MVC项目,并附加了一个自定义UserManager,UserStore,与Nhibernate一起使用的SignInManager, 当我试图授权应用程序抛出异常 如何解决?谷歌的许多文章都没有帮助我

  

InvalidOperationException:没有配置IAuthenticationSignInHandler来处理该方案的登录:Identity.Application   Microsoft.AspNetCore.Authentication.AuthenticationService + d__13.MoveNext()   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

            var result = await this.SignInManager.PasswordSignInAsync("960224350816", "ASD123qwe", false,false);



            public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,options =>
                    {
                        options.ExpireTimeSpan = TimeSpan.FromDays(7);
                    }
                );
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddIdentityCore<User>(options => {
                options.Password.RequiredLength = 10;
                }).AddSignInManager<ApplicationSignInManger<User>>().AddUserManager<ApplicationUserManager<User>>().AddUserStore<ApplicationUserStore>().AddDefaultTokenProviders();
            services.AddScoped<SignInManager<User>, ApplicationSignInManger<User>>();
            services.Configure<IdentityOptions>(options =>
            {
                // Password settings
                options.Password.RequireDigit = true;
                options.Password.RequiredLength = 8;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = true;
                options.Password.RequireLowercase = false;
                options.Password.RequiredUniqueChars = 6;

                // Lockout settings
                options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
                options.Lockout.MaxFailedAccessAttempts = 10;
                options.Lockout.AllowedForNewUsers = true;

                // User settings
                options.User.RequireUniqueEmail = true;
            });
            services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                options.Cookie.Expiration = TimeSpan.FromDays(150);
                options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
                options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
                options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
                options.SlidingExpiration = true;
            });
            services.AddMvc();
        }

并配置

       public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseStaticFiles();

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

    }

控制器中的操作:

        public async Task<IActionResult> Index() {
        try {
            var result = await this.SignInManager.PasswordSignInAsync("960224350816", "ASD123qwe", false, false);
        }
        catch (Exception ex) {
            var x = 0;
        }


        return View();
    }

1 个答案:

答案 0 :(得分:1)

最近有同样的问题。 为IdentityConstants.ExternalScheme添加Cookie解决了它

services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
            options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
            options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
        }).AddCookie(IdentityConstants.ApplicationScheme, options =>
        {
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromHours(1);
            options.LoginPath = "/Account/Signin";
            options.LogoutPath = "/Account/Signout";
        }).AddCookie(IdentityConstants.ExternalScheme, o =>
        {
            o.Cookie.Name = IdentityConstants.ExternalScheme;
            o.ExpireTimeSpan = TimeSpan.FromMinutes(5.0);
        });