Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting login path within ASP.NET Core 2.2 MVC and Identity

Project details:

  • ASP.NET Core 2.2 MVC
  • Identity

I created an own login view. Actually if I call a view, for which I have to be authorized (by authorize attribute), it will automatically redirected to the login view. But at the moment it's "/Identity/Account/Login". But I want, that my own login view is shown. How can I do that?

I already tried this within startup.cs:

services.AddDefaultIdentity<ApplicationUser>(config => { config.SignIn.RequireConfirmedEmail = true; })
    .AddDefaultUI(UIFramework.Bootstrap4).AddEntityFrameworkStores<ApplicationDbContext>();

services.Configure<IdentityOptions>(options => { options.User.RequireUniqueEmail = true; });
services.ConfigureApplicationCookie(options =>
    {
        options.Cookie.Name = "auth_cookie";
        options.AccessDeniedPath = "/Account/Login";
        options.LoginPath = "/Account/Login";
        options.LogoutPath = "/Account/LogOff";
        options.Cookie.HttpOnly = true;
        options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
        options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
        options.SlidingExpiration = true;
    });
like image 286
everydayXpert Avatar asked Dec 11 '22 03:12

everydayXpert


1 Answers

You could try to configure CookieAuthenticationOptions to achieve your requirement.

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    services.AddDefaultIdentity<IdentityUser>()
        .AddDefaultUI(UIFramework.Bootstrap4)
        .AddEntityFrameworkStores<ApplicationDbContext>();

    services.PostConfigure<CookieAuthenticationOptions>(IdentityConstants.ApplicationScheme,
    opt =>
    {
        //configure your other properties
        opt.LoginPath = "/Login";
    });
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
like image 62
Edward Avatar answered Jan 23 '23 06:01

Edward