0

I'm working with an MVC application and Windows Authentication. I've already implemented a custom role provider which will return an array of roles that it is now pulling from my database.

Custom Role Provider (plain)

public class CustomRoleProvider : RoleProvider
{       
    ...other methods removed...

    //Custom Override
    public override string[] GetRolesForUser(string username)
    {
        //Get custom roles from username
        using (MyDBEntities db = new MyDBEntities ())
        {
            var roles = (from p in db.MyProcedure(username)
                         select p.UserRole).ToArray();

            if (roles != null)
            {
                return roles;
            }
            else
            {
                return new string[] { };
            }
        }
    }
}

What I would like to do next is some cleanup on the username that is being returned. Right now I'm doing this within the GetRolesForUser method. The first thing I do is strip off the "company//" from the domain, then I remove a certain prefix that is used, then I run the username through a testing procedure we have, which will return an impersonated username if the application is being tested.

Don't worry about the specifics of all of that. All I'd like to do is to move that cleanup code to a custom membership provider in the GetUser method and return the username like I want it. How can I implement that?

Custom Role Provider (with extra code)

public class CustomRoleProvider : RoleProvider
{       
    ...other methods removed...

    //Custom Override
    public override string[] GetRolesForUser(string username)
    {
        //Strip off the domain and lower text
        username = username.ToString().ToLower().Split('\\')[1];

        //remove company prefix
        if (username.Contains("somePrefix-"))
        {
            username = username.Substring(username.IndexOf("somePrefix-") + 11);
        }

        //Return impersonated username if being tested
        MyRepository myRepository = new MyRepository();
        username = myRepository.GetUserName(username);

        //I want to move out all of that code from above


        //Get custom roles from username
        using (MyDBEntities db = new MyDBEntities ())
        {
            var roles = (from p in db.MyProcedure(username)
                         select p.UserRole).ToArray();

            if (roles != null)
            {
                return roles;
            }
            else
            {
                return new string[] { };
            }
        }
    }
}

Custom Membership Provider

I'd like to move that cleanup code here, so after login, the username is returned how I want it.

public class CustomMembershipProvider : MembershipProvider
{
    ...other methods removed...

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        //I'd like to move my custom code here

        //Strip off the domain and lower text
        username = username.ToString().ToLower().Split('\\')[1];

        //remove company prefix
        if (username.Contains("somePrefix-"))
        {
            username = username.Substring(username.IndexOf("somePrefix-") + 11);
        }

        //Return impersonated username if being tested
        MyRepository myRepository = new MyRepository();
        username = myRepository.GetUserName(username);

        //So now that's I've formatted the username as I want it, 
        //how can I return that as part of the MembershipUser return type?
    }
madvora
  • 1,717
  • 7
  • 34
  • 49
  • Your question is confusing. ASP.NET Identity and Membership are *two entirely separate frameworks*, Identity being the most current of the two. You are creating membership providers, which have absolutely nothing to do with Identity. For customizing identity, see [Overview of Custom Storage Providers for ASP.NET Identity](http://www.asp.net/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity). – NightOwl888 Jul 05 '16 at 21:12
  • Thanks. Sorry for the confusion, I removed the word Identity from the title. – madvora Jul 05 '16 at 21:29
  • No problem. It is a bit of a curiosity why you are customizing an old framework like Membership in the modern era, though. Perhaps you should add some information explaining that this is a legacy application and you have no other choice, and need to use the old .NET 2.0 features for some reason. – NightOwl888 Jul 05 '16 at 22:22
  • Well, I have an MVC application that uses Windows Authentication. I want to be able to restrict access to certain controllers and methods with the [Authorize(Roles = "admin")]. The roles happen to be stored in SQL tables, which is what I'm using the custom role provider stuff for. I'd really prefer to use Identity, but I've only used that with Individual Accounts before and don't see a lot of info out there about using it with Windows Authentication and custom roles. – madvora Jul 05 '16 at 22:54

0 Answers0