0

Building an MVC5 application and I am using my custom authorization with windows authentication. The live project is authenticating from an active directory. During development I want the anonymous user to access the app as administrator as that would help me during the development process.

My custom class:

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        #if DEBUG
            //TODO: set anonymous user as administrator during developement
            return true;
        #else
            return base.AuthorizeCore(httpContext);
        #endif
    }
}

This article helped me a lot but I just need to add the admin role if possible.

Is there a way to add a role to the anonymous user during the development process? For me to use the attribute as such in debug mode

User.IsInRole("Admin") == true
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
gardarvalur
  • 1,565
  • 9
  • 39
  • 65

1 Answers1

-1

You can create a user in the startup.cs.

if (_user == null){
                var user = new ApplicationUser
                {
                    UserName = "Administrator",
                    Email = "youremail",
                    RoleId = 1
                };

                var result = await UserManager.CreateAsync(user,"yourpassword");
                UserManager.AddToRole(user.Id, "Admin");
            }
Kcj
  • 1
  • 1