ASP .NET Core Identity SignInManager

旧城冷巷雨未停 提交于 2021-01-28 08:52:12

问题


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

Then in view checking:

SignInManager.IsSignedIn(User)

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.

Upd. Configuration in 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?}");
        });
    }

回答1:


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



回答2:


I had the same problem but after checking your post, I realised I haven't included the authentication. I didn't need to set my cookies.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // ...

    app.UseAuthentication();

    // ...
}

I thought I'll answer this question in case someone else comes across it having faced the same problem.



来源:https://stackoverflow.com/questions/49571713/asp-net-core-identity-signinmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!