0

I am trying to use a simple custom role provider and using the code from here: http://code.google.com/p/sfckopanka/source/browse/trunk/App_Code/OdbcRoleProvider.cs?r=45

Which is implemented using this: http://msdn.microsoft.com/en-us/library/tksy7hd7(v=vs.100).aspx

This is all just simple boilerplate code from Microsoft.

When I debug my app I can see that my Role Provider is initialized BUT no methods are ever called when I try to check roles.

[Authorize(Roles="Customer")]

or

User.IsInRole("Customer")

I put break points in several places in my role provider and they are just never hit.

FYI I am using WebAPI and I am not using a Membership Provider, instead I am using Basic Auth via a message handler.

http://www.piotrwalat.net/basic-http-authentication-in-asp-net-web-api-using-message-handlers/

The Basic Auth is working great, but I ma not sure if this is what is preventing my Role Provider from being called.

Slee
  • 27,498
  • 52
  • 145
  • 243

1 Answers1

0

Answering this in case it can help someone else. In the Basi Auth code linked above there there is a PrincipalProvider class. in this class you create a GenericPrincipal, which also takes the roles that the user is in, so I just had to add a line of code to get my roles to provide to the GenericPrincipal

     public IPrincipal CreatePrincipal(string username, string password)
            {

                if (!MyRepo.Authentication.ValidateUser(username, password))
                {
                    return null;
                }

                var identity = new GenericIdentity(username);

              //Code to get my roles from my role provider to use when setting principal
                string[] roles =Roles.Provider.GetRolesForUser(username);

                IPrincipal principal = new GenericPrincipal(identity,roles);

                ShopZioRepo.ClearUserCache(ShopZioGlobal.MyCookies.UserID);
                var user = ShopZioRepo.GetUserByEmail(username);
                ShopZioGlobal.MyCookies.UserID = user.SalesRepID;
                ShopZioGlobal.MyCookies.Username = username;


                return principal;
            }

Hope this helps someone.

Slee
  • 27,498
  • 52
  • 145
  • 243