InvalidOperationException: No IAuthenticationSignI

2019-08-21 14:22发布

Hi. I create new AspNet core MVC project AND attached to it a custom UserManager,UserStore,SignInManager that work with Nhibernate, and when i am trying to auth application throw exception How to Solve it ? many articles in google dont help me

InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationService+d__13.MoveNext() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

            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();
        }

And Configure

       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?}");
        });

    }

Action in Controller :

        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条回答
神经病院院长
2楼-- · 2019-08-21 15:03

Recently had the same problem. Adding Cookie for IdentityConstants.ExternalScheme solved it

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);
        });
查看更多
登录 后发表回答