asp.net-mvcencryptionasp.net-mvc-5membership-providerroleprovider

Encrypted UserName In GetRolesForUser RoleProvider


I Encrypt and Decrypt Data with Key and Iv, and encryepted UserName.(create a key and iv for each user register on my site) so i need to specify key and iv for decrypt each username

GetRolesForUser method in RoleProvider only have one parameter (username)

public override string[] GetRolesForUser(string username)
        {
            var roles = (from u in db.Users
                join r in db.Roles on u.RoleId equals r.Id
                where u.UserName == username
                select r.RoleNameInSystem).ToArray();
            return roles;
        }

Above code work fine but when data are not encrypted.

But for Decrypt username need to Key and Iv each user

I have no idea for solution


Solution

  • I found the solution, in controller i access to Key and Iv ,when user login on my site i encrypt user name and pass to FormsAuthentication.SetAuthCookie ,The task of this method is send username to RoleProvider class (GetRolesForUser method).

    It is important to note that: User.Identity.Name return Encrypted username