Search code examples
c#.netasp.net-mvcasp.net-coreasp.net-identity

How to set Login.cshtml as startup page in .NET Identity Framework?


I'm trying to learn .NET Identity Framework. As you know Identity Framework's default pages doesn't need controller actions to work. So I can't view this pages like I did with my custom pages that has related actions in controllers. I want Login.cshtml page to load when I run my application. My Login.cshtml file is under "Areas/Identity/Pages/Account/" directory.

Program.cs file:

using IdentityStudy.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddDbContext<StudyDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("ConString")));

builder.Services.AddDefaultIdentity<IdentityUser>().AddDefaultTokenProviders().
    AddRoles<IdentityRole>().
    AddEntityFrameworkStores<StudyDbContext>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapFallbackToPage("/Areas/Identity/Pages/Account/Login", "/Identity");
});

app.Run();

Solution

  • As you know Identity Framework's default pages doesn't need controller actions to work. So I can't view this pages like I did with my custom pages that has related actions in controllers.

    Yes, the default identtiy page is inside the identityUI package, but we still could create it by using the scrofold tool inside the visual stuido.

    enter image description here

    enter image description here

    I want Login.cshtml page to load when I run my application. My Login.cshtml file is under "Areas/Identity/Pages/Account/" directory.

    One easy way is using the app.mapget method to achieve it. You could put it like below codes inside the program.cs to let the client redirect to the Identity/Account/Login.

    ...
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseMigrationsEndPoint();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthorization();
    
    app.MapGet("/", async context =>
    {
        context.Response.Redirect("/Identity/Account/Login");
    });
    
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    app.MapRazorPages();
    
    app.Run();