30

I am using the new Identity UI package available since ASP.NET Core 2.1 was released. Using a newly generated MVC project, here are some page URLs that are available:

/Home/About
/Home/Contact
/Identity/Account/Login
/Identity/Account/Register

How can I configure routing to remove the /Identity/ part from the URLs?

3
  • 1
    Guys, ASP.NET Core 2.0 has new syntactic sugar @page "/Some/Other/Path" which enables you to remove either "/Identity" or "/Identity/Account". See learn.microsoft.com/en-us/aspnet/core/?view=aspnetcore-2.0 for reference: "Razor Pages makes coding page-focused scenarios easier and more productive". BlackTigerX should be an accepted answer. May 11, 2019 at 15:12
  • good answer @page "/some/path"
    – Moaz Salem
    Nov 14, 2019 at 1:30
  • @AntonLyhin: The link is dead.
    – Poul Bak
    Aug 9, 2021 at 15:03

7 Answers 7

17

It looks like this is not yet possible. Looking at the source code, it's clear that the Area name is hardcoded in IdentityDefaultUIConfigureOptions<TUser>:

private const string IdentityUIDefaultAreaName = "Identity";

This is used in a handful of places, including when configuring Razor Pages. e.g.:

options.Conventions.AuthorizeAreaFolder(IdentityUIDefaultAreaName, "/Account/Manage");

And also when configuring the Cookies authentication. e.g.:

options.LoginPath = $"/{IdentityUIDefaultAreaName}/Account/Login";

It's worth noting that IdentityDefaultUIConfigureOptions<TUser> itself is protected, so the ability to override the options does not appear to exist.

I've opened a Github issue to see if we can get feedback from those involved in the project itself.


2018-06-12 Update

Javier Calvarro Nelson from the ASP.NET Core Identity team provided some valuable feedback in the Github issue I raised, which can be summarised as follows:

The main reason for the Identity UI to be in an area is to minimize the impact on your app and to provide a clean separation between your app code and the Identity code.

Javier recommends one of the following options when wanting to customise the URLs:

  • Use the scaffolding element of the Default UI and make all necessary customisations yourself.
  • Use a redirection rule that points the old routes to the new routes.
  • Don't use the Default UI at all.

Although unsupported and not recommended, Javier also points out that it is possible to use a custom IPageApplicationModelConvention to override the URLs. However, in case you missed it, this is unsupported and not recommended.


2018-06-27 Update

The official documentation has now been updated to better explain said URL changes.

2
  • AFAIK, the scaffolding is essentially just the views, not the controllers, so scaffolding Identity doesn't really open any new options than .AddDefaultUi as far as behavior/routing is concerned. The only option that still uses the IdentityUi is to add a mapping rule to work around the issue; see UnknownQuestion's response here. Jun 27, 2018 at 16:35
  • It has a negative impact on your SEO to have two domains with the same content. Instruct crawlers not to index /identity/ User-agent: * Disallow: /identity/ and use the rewriting middleware with a perm redirect to the new url.
    – Hamed
    Sep 7, 2020 at 9:23
8

The easiest thing to do, is to drag the Pages folder out of the Areas/Identity to the main project Remember that the @page directive (in the .cshtml) causes the views to be accessible directly for anything under "Pages" (the page is turned into an action) You could also rename the Account folder to some other name if you wanted to change the default /Account/Login etc pages

the @page directive can also be used to specify a custom path, such as: @page "/Login"

to have access to the login page directly by navigating to /Login

1
  • When you move Pages folder to another area, /Account/Login still works and it brings up the default Identity UI. Dec 2, 2023 at 13:27
6

In your startup.cs you can change :

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

with :

    services.AddMvc().AddRazorPagesOptions(o => o.Conventions.AddAreaFolderRouteModelConvention("Identity", "/Account/", model =>
    {
        foreach (var selector in model.Selectors)
        {
            var attributeRouteModel = selector.AttributeRouteModel;
            attributeRouteModel.Order = -1;
            attributeRouteModel.Template = attributeRouteModel.Template.Remove(0, "Identity".Length);
        }
    })
).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

It will route:

/Identity/Account/Login to /Account/Login

/Identity/Account/Register to /Account/Register

etc...

To handle the ReturnUrl you can create a new Action:

    [Route("Identity/Account/Login")]
    public IActionResult LoginRedirect(string ReturnUrl)
    {
        return Redirect("/Account/Login?ReturnUrl=" + ReturnUrl);
    }
0
4

According to David's answer I was able to get rid of "Identity/Account" in login page route. I am working on a Asp.net 3.1 application and using scaffold-ed Identity pages.

I have changed the @page directive in login.cshtml page like below and changed the startup.cs file as below.

login.cshtml

@page "/Login"

startup.cs

services.ConfigureApplicationCookie(options =>
        {
            // httponly cookie
            options.Cookie.HttpOnly = true;
            options.Cookie.Name = "application.Identity";
            ***options.LoginPath = "/Login";*** // earlier it was options.LoginPath = "/Identity/Account/Login";
            options.AccessDeniedPath = "/Error/Index/403";
            options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
            options.SlidingExpiration = true;
        });

Also please note that if you are using "/Identity/Account/Login" in any where in your application you might need to change them as well. Also if you have MVC route as the same you have to be more carefully and handle accordingly.

2

URL Rewriting Middleware may be a solution:

var options = new RewriteOptions()
        .AddRewrite(@"^Account/(.*)", "Identity/Account/$1", skipRemainingRules: true);
        app.UseRewriter(options);
1

As of .net core 3.1 and 2021 (I know that there's .net 5, guilty as charged) and although it wouldn't generally remove the "identity" part of the path, you can map generated razor pages one by one to desired routes:

in the page header, there is by default just @page, so change it to your desired route @page "/login".

Please note, that if some MVC route matches route /login, it would use the corresponding controller action. If you don't want your MVC route to override razor pages routing, I would suggest moving endpoints.MapRazorPages(); in Configure method of Startup.cs above all (or at least some) MVC routes.

0

As far as the routing goes, its standard in web frameworks to have the authentication URLs fixed, Django does the same thing. Here's how to customize the view to your liking, so instead of removing /Identity/ from the routes, we'll tell Identity not to include their views and provide the route to ours.

Go to Startup.cs:

// USE METHOD WITH LESS DEFAULTS
//
// services.AddDefaultIdentity<IdentityUser>()
//    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();
//
// ADD A ROUTE BELOW THE DEFAULT ROUTE
//
routes.MapRoute(
            name: "identity",
            template: "Identity/{controller=Account}/{action=Register}/{id?}");

Now we have everything setup but the view, so we need to make a route to it the way we normally do in mvc. Make an Account Controller. Change Index() to Register(). Make a folder in views named Account. Add a file Register.cshtml, here's the original html, customize to your needs:

<div class="container body-content">


<h2>Register</h2>

<div class="row">
    <div class="col-md-4">
        <form method="post" action="/Identity/Account/Register" novalidate="novalidate">
            <h4>Create a new account.</h4>
            <hr>
            <div class="text-danger validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div>
            <div class="form-group">
                <label for="Input_Email">Email</label>
                <input class="form-control" type="email" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Input_Email" name="Input.Email" value="">
                <span class="text-danger field-validation-valid" data-valmsg-for="Input.Email" data-valmsg-replace="true"></span>
            </div>
            <div class="form-group">
                <label for="Input_Password">Password</label>
                <input class="form-control" type="password" data-val="true" data-val-length="The Password must be at least 6 and at max 100 characters long." data-val-length-max="100" data-val-length-min="6" data-val-required="The Password field is required." id="Input_Password" name="Input.Password">
                <span class="text-danger field-validation-valid" data-valmsg-for="Input.Password" data-valmsg-replace="true"></span>
            </div>
            <div class="form-group">
                <label for="Input_ConfirmPassword">Confirm password</label>
                <input class="form-control" type="password" data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="*.Password" id="Input_ConfirmPassword" name="Input.ConfirmPassword">
                <span class="text-danger field-validation-valid" data-valmsg-for="Input.ConfirmPassword" data-valmsg-replace="true"></span>
            </div>
            <button type="submit" class="btn btn-default">Register</button>
        <input name="__RequestVerificationToken" type="hidden" value="CfDJ8IWbPHM_NTJDv_7HGewWzbbRveP09yQOznYdTWL2aN5X_4_eVbNE1w8D_qz7zegloVtdAhuVOJbJLQo0ja73FB3PgYycyGpn-DfX3fJqv4Cx8ns6Ygh6M7nMxV0eozO7hoDxUfPwrIJb2RcFtyzhPpMevZ4P0M8aVyBP55SP-5C4l23dCtDXXUOAY_YLwt67dw"></form>
    </div>
</div>


        <hr>
        <footer>
            <p>© 2018 - SqlServerApp</p>
        </footer>
    </div> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.