.net-6.0basic-authentication.net-7.0role-base-authorization

Authentication Issue on Downgrading from dotnet 7 to dotnet 6


I have a razor pages project based on .NET 7 and using cookies based authentication - Authorization system. Due to some reassons I need to downgrade my projecr from dotnet 7 to 6.

After Downgrading my project I am getting this error on the pages where Authorization is required Error Image Image of Error

I am also sharing my Program.cs file code here please check and let me know how I can make this code working on .NET 6 Program.cs

using MCI.Data;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSession();
builder.Services.AddRazorPages();
builder.Services.AddHttpContextAccessor();

builder.Services.AddAuthentication().AddCookie("MyLockIdentity", options =>
{
    options.Cookie.Name = "MyLockIdentity";
    options.LoginPath = "/inside/Lock";
    options.AccessDeniedPath = "/AccessDenied";
    //Admin will logout after 10 minutes.
    options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
});

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("AuthorizAdmin", policy => policy.RequireRole("Admin"));
});


var ConString = builder.Configuration.GetConnectionString("constring").ToString();

builder.Services.AddMvc();
builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(ConString));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/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.UseSession();
app.UseRouting();
app.UseAuthentication();    
app.UseAuthorization();

app.MapRazorPages();

app.Run();`

Note: Every thhing start working fine just after switching to .NET7 in csproj files tag.

I want this code to work on .NET6 like it is working on .NET7 Already


Solution

  • I sort this issue by Making follwing changes in the code.

    Old Code in Program.cs

        builder.Services.AddAuthentication().AddCookie("MyLockIdentity", options =>
    {
        options.Cookie.Name = "MyLockIdentity";
        options.LoginPath = "/admin/login";
        options.AccessDeniedPath = "/AccessDenied";
        //Admin will logout after 10 minutes.
        options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
    });
    

    New Code in Program.cs

        builder.Services.AddAuthentication("Cookies").AddCookie("MyLockIdentity", options =>
    {
        options.Cookie.Name = "MyLockIdentity";
        options.LoginPath = "/admin/login";
        options.AccessDeniedPath = "/AccessDenied";
        //Admin will logout after 10 minutes.
        options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
    });
    

    Added the string "Cookies" in AddAuthentication() method.

    in Login Page I did this

      await HttpContext.SignInAsync("Cookies", claimsPrincipal);