简体   繁体   English

ASP.NET CORE MVC IDENTITY 如何更改默认登录页面的 url?

[英]ASP.NET CORE MVC IDENTITY How to change url of defualt log in page?

I have authentification using Identity.我使用身份进行身份验证。 But if I use [Authorize] atribute, I`m redirected to localhost:7174/Identity/Account/Login?ReturnUrl=%2FUser%2FUser while I don't have such page.但是如果我使用 [Authorize] 属性,我会被重定向到 localhost:7174/Identity/Account/Login?ReturnUrl=%2FUser%2FUser 而我没有这样的页面。 How to change this url?如何更改此 url? I didn't find any methods or properties that can change that.我没有找到任何可以改变它的方法或属性。 Also, I added that Identity pages with Scaffold.此外,我还添加了带有 Scaffold 的 Identity 页面。 But even now it said that it's impossible to reach such page.但是即使现在它也说不可能到达这样的页面。 What should I better do?我应该怎么做更好?

I don't know what could be usefull for you, so here is my program.cs我不知道什么对你有用,所以这是我的 program.cs

using DreamWeb.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;


var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMvc();
builder.Services.AddControllersWithViews(); 
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddDbContext<DreamsContext>(options =>
    options.UseSqlServer("server = SCAT\\SQLEXPRESS; database = dreams_web; Trusted_Connection=True ; MultipleActiveResultSets = true"));
builder.Services.AddDefaultIdentity<UserAccount>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<DreamsContext>();
builder.Services.AddAuthentication().AddCookie();


builder.Services.Configure<IdentityOptions>(options =>
{
    options.SignIn.RequireConfirmedPhoneNumber = false;
    options.SignIn.RequireConfirmedEmail = false;
    options.SignIn.RequireConfirmedAccount = false;

    options.Password.RequireUppercase = false;
    options.Password.RequireDigit = false;
    options.Password.RequiredLength = 1;
    options.Password.RequireLowercase = false;
    options.Password.RequireNonAlphanumeric = false;
});

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

If you look here in the documentation there seems to be an option you can pass in on startup that allows you to set that url names "loginUrl"如果您在文档中查看此处似乎有一个选项可以在启动时传递,允许您设置 url 名称“loginUrl”

UserInteraction LoginUrl, LogoutUrl, ConsentUrl, ErrorUrl, DeviceVerificationUrl Sets the URLs for the login, logout, consent, error and device verification pages. UserInteraction LoginUrl、LogoutUrl、ConsentUrl、ErrorUrl、DeviceVerificationUrl 设置登录、注销、同意、错误和设备验证页面的 URL。

so reasonably you should be able to do所以合理地你应该能够做到

public void ConfigureServices(IServiceCollection services)
{
    ...
    var builder = services.AddIdentityServer(
      options =>
        {
            ...
            options.loginUrl = "myLoginUrl"
        }
    );
    ...
}

About changing default login path.关于更改默认登录路径。 The best way I've found is changing it with CookieAuthenticationOptions in my program.cs:我发现的最好方法是在我的 program.cs 中使用 CookieAuthenticationOptions 更改它:

builder.Services.PostConfigure<CookieAuthenticationOptions>(IdentityConstants.ApplicationScheme, options =>
{
    options.LoginPath = "/SignIn";
});

Attempts to change it in other way (with identity server) had no success.尝试以其他方式(使用身份服务器)更改它没有成功。

This helped me: Setting login path within ASP.NET Core 2.2 MVC and Identity .这对我有帮助: 在 ASP.NET Core 2.2 MVC 和 Identity 中设置登录路径

As for second question (about Identity scaffolded pages, that cant be reached), I was missing this in my program.cs:至于第二个问题(关于身份脚手架页面,无法访问),我在我的 program.cs 中错过了这个:

app.MapRazorFiles();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM