3

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. How to change this url? I didn't find any methods or properties that can change that. Also, I added that Identity pages with Scaffold. 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

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();
3
  • You can specify additional options for added service, login/logout path in that case: learn.microsoft.com/en-us/dotnet/api/… . Same for older ones. Commented Jan 4, 2022 at 21:37
  • As for the 2nd question, You never add those added pages to the route. Upadte MapControllerRoute with additional entries. You can use MapControllers if you like to move from the table to attribute based routing. Commented Jan 4, 2022 at 21:49
  • Hello are you still having the issue? Did you tried the solution provided? Commented Jan 5, 2022 at 2:57

2 Answers 2

2

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"

UserInteraction LoginUrl, LogoutUrl, ConsentUrl, ErrorUrl, DeviceVerificationUrl Sets the URLs for the login, logout, consent, error and device verification pages.

so reasonably you should be able to do

public void ConfigureServices(IServiceCollection services)
{
    ...
    var builder = services.AddIdentityServer(
      options =>
        {
            ...
            options.loginUrl = "myLoginUrl"
        }
    );
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

About changing default login path. The best way I've found is changing it with CookieAuthenticationOptions in my program.cs:

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.

As for second question (about Identity scaffolded pages, that cant be reached), I was missing this in my program.cs:

app.MapRazorFiles();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.