Role based authorization attribute not working in ASP.NET Core MVC

Viewed 562

I am trying to build an application in ASP.NET Core MVC and use a role based authorization with windows authentication.

Even though I have added all roles in HttpContext on my base controller, it is still not throwing unauthorize prompt/error when navigating to a controller with my account not having the required role.

The only issue I'm thinking is possibly I didn't configure the authorization correctly at the Startup.cs?

Here's my Startup ConfigureService:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<CimsDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDatabase")));

    services.AddControllersWithViews();
    services.AddDistributedMemoryCache();
    services.AddAuthentication(IISDefaults.AuthenticationScheme);
    services.AddAuthorization();


    services.AddSingleton(Configuration);
    services.AddSession(options =>
    {
        options.Cookie.Name = ".MySession";
        options.IdleTimeout = TimeSpan.FromMinutes(Convert.ToDouble(Configuration.GetSection("inMinutes").Value));
        options.Cookie.HttpOnly = true;
        options.Cookie.IsEssential = true;
    });
}

Here's my Startup Configure:

public void Configure(IApplicationBuilder app)
{
    if (_env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseSession();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}           

Then my base controller class:

[AllowAnonymous]
public class AppSessionController : Controller
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (HttpContext.User.Identity.IsAuthenticated)
        {
            //no valid session / create new session
            if (String.IsNullOrEmpty(HttpContext.Session.GetString(AppSessionIdentifier.IsAlive)))
            {
                //initialize new user identity
                HttpContext.User = UserIdentity.NewUserIdentity(config, HttpContext);
            }
        }
        base.OnActionExecuting(filterContext);
    }
}

And next is my NewUserIdentity function:

public static GenericPrincipal NewUserIdentity(IConfiguration config, HttpContext httpContext)
{
    
    string AccountName = httpContext.User.Identity.Name.Split('\\')[1];
    string Env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

    List<string> roles = new List<string>();
    if (ADHelper.IsInGroup(AccountName, config.GetSection($"ADGroupRoles:{Env}:ReadOnly").Value))
        roles.Add(Roles.ReadOnly.ToString());
    if(ADHelper.IsInGroup(AccountName, config.GetSection($"ADGroupRoles:{Env}:Admin").Value))
        roles.Add(Roles.Admin.ToString());

    return new GenericPrincipal(new GenericIdentity(AccountName), roles.ToArray());
}

My controller with Authorize attribute:

    [Authorize(Roles = "ReadOnly")]
    public class MatterController : AppSessionController
    {
        [Authorize(Roles = "ReadOnly")]
        public ActionResult Details(int matterId)
        {
            return View();
        }
   }

My Role enums:

public enum Roles
{
    [Description("ReadOnly")]
    ReadOnly = 1,

    [Description("Administrator")]
    Admin = 4
}
1 Answers

Okay, I was able to figure it out and as King said, I just need to remove the Anonymous attribute in my base controller. Though my main issue was, when I run my web app in IIS Express launched in Chrome, Windows Authentication is not working. So I need to switch to either IE or MS Edge. Luckily, MS Edge is very similar with Chrome so I didn't lose any web development tools I need in Chrome.

TLDR: Windows Authentication is not working in IIS Express launched in Chrome, only in IE and MS Edge.

Related