Search code examples
c#asp.net-corerazor-pages

How Do I set login path in razor pages core?


Why I am getting,  The path in 'value' must start with '/'. (Parameter 'value')

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = "/Login";
                });
ArgumentException: The path in 'value' must start with '/'. (Parameter 'value')

enter image description here

enter image description here


Solution

  • Why I am getting, The path in 'value' must start with '/'. (Parameter 'value')

    In my test, I can reproduce the issue once I ommit the "/" from the login path. As you can see the error I reproduce:

    enter image description here

    I have tried in following way:

    Login Convention I set:

    builder.Services.AddMvc().AddRazorPagesOptions(options =>
    {
        options.Conventions.AllowAnonymousToPage("/Login");
    });
    

    Cookie Authentication Configuration:

    builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                    .AddCookie(options =>
                    {
                        options.LoginPath = "/Login";
                    });
    

    Middleware I am using:

    app.UseDeveloperExceptionPage();
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.MapRazorPages();
    app.UseAuthentication();
    

    Output:

    enter image description here