ASP .NET Core身份SignInManager [英] ASP .NET Core Identity SignInManager

查看:93
本文介绍了ASP .NET Core身份SignInManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天.ASP.NET-Core项目未进行身份验证.因此,我为此尝试添加了内置身份.数据库中的表已成功创建,新用户已注册.在登录方法 SignInManager 中的帐户控制器中,返回成功的过程.在局部视图中,其中Login \ Register的链接添加了注入和库(根据其实现,所有这些都看起来是带有身份验证的默认项目):

Good day. ASP.NET-Core project was with out authentication. So, I tried to add built-in Identity for this purpose. Tables in database successfully created, new user registered. In Account controller in Login Method SignInManager return success in process. In partial view, where links for Login\Register added injection and library (all looks as default project with authentication, based on its implementation):

@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager

然后在视图中检查:

SignInManager.IsSignedIn(User)

登录后始终为假,无关紧要的是,控制器之前的登录状态为成功.我是否错过了在 Startup.cs 中的 Configuration 处添加其他内容的想法?通过身份验证实现了默认VS项目中的所有功能.

After Login it's always false, irrelevant moment ago state for login in controller was success. Have I missed to add something at Configuration in Startup.cs or some else? Implemented all things as in default VS project with authentication.

已更新.在 Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
        options.UseMySql("Server=; Database=; Uid=;Pwd=;"));

        services.AddIdentity<ApplicationUser, IdentityRole>(options =>
        {
            options.Password.RequireDigit = true;
            options.Password.RequiredLength = 6;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = true;
            options.Password.RequireLowercase = false;

            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;

            options.User.RequireUniqueEmail = true;
            options.SignIn.RequireConfirmedEmail = true;
        })
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Here added my application services. 
        .......

        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

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

推荐答案

在您的ConfigureServices方法中,还配置cookie.如下所示:

In your ConfigureServices method configure cookies as well. Something like below:

...
// Here added my application services. 
.......
services.ConfigureApplicationCookie(options => 
{
     options.Cookie.HttpOnly = true;
     options.Cookie.Expiration = TimeSpan.FromDays(5);
     options.LoginPath = "/Account/Login";
 });

这篇关于ASP .NET Core身份SignInManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆