0

I'm using windows authentication and custom roles. I've extended the WindowsPrincipal because I want to include additional information about the user based on a User class I've added. When I run the application, it sees the CustomPrincipal assigned to the built-in User, but not the additional "user" property I've added. I'm sure I'm doing something really dumb, but this is my first run into the C# ASP world and could really appreciate some help. Here is my custom principal and global.asax

Custom principal:

    public class CustomPrincipal : WindowsPrincipal
{
    List<string> _roles;
    private User thisUser = new User();


    public CustomPrincipal(WindowsIdentity identity)
        : base(identity)
    {
        _roles = new List<string>(Roles.GetRolesForUser());
        user = thisUser.getDarUser(identity.Name);

    }

    public User user { get; private set; }

    public override bool IsInRole(string role)
    {
        if (base.IsInRole(role) || _roles.Contains(role) || _roles.Contains("Admin") || _roles.Contains("Dev"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

And Global.asax:

 protected void Application_AuthorizeRequest(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            IIdentity thisId = User.Identity;
            WindowsIdentity wi = (WindowsIdentity)thisId;
            CustomPrincipal cp = new CustomPrincipal(wi);
            HttpContext.Current.User = cp;
            Thread.CurrentPrincipal = cp;
        }
    }

Thanks again for any direction.

1 Answers1

1

Looking into it a little more I found the fail was in how I was trying to access the principal in my views. Thanks to ASP.NET MVC - Set custom IIdentity or IPrincipal, while this was not related to my Windows Auth type project, it did lead me to the correct usage of my principal.

What I was doing WRONG:

@User.user.myproperty

Change to:

@((User as CustomPrinicpal).user.myproperty

Hopefully this helps another newb not make the same bonehead mistake

Community
  • 1
  • 1