gpt4 book ai didi

asp.net-mvc - 在 ASP.NET Core 2.2 MVC 和 Identity 中设置登录路径

转载 作者:行者123 更新时间:2023-12-03 23:28:42 24 4
gpt4 key购买 nike

项目详情:

  • ASP.NET Core 2.2 MVC
  • 身份

  • 我创建了一个 自己的登录 View .实际上,如果我调用一个 View ,我必须获得授权(通过 授权属性 ),它将自动重定向到登录 View 。但目前它是“/Identity/Account/Login”。但是 我希望显示我自己的登录 View .我怎样才能做到这一点?

    我已经在 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;
    });

    最佳答案

    您可以尝试配置 CookieAuthenticationOptions达到您的要求。

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

    关于asp.net-mvc - 在 ASP.NET Core 2.2 MVC 和 Identity 中设置登录路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54959280/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com