Build a fully customized MembershipProvider and RoleProvider

The problem:

UsreId is widely used in all kind of web applications. The question is how you can pass the userId to different location in your web application. By using Session variable? Make something like Session["UID"]=blahblahblah? Oh come on, it is 21st century already and we need something new. Session variable is a cool choice; I use it a lot before I switch to the custom MembershipProvider and the RoleProvider.

The main drawback of Session is that it can be lost due to variety reasons. Once your Session is gone, everything stored in it is gone too.

Take a quick look on this line of code.

var userName = User.Identity.Name;

Is it possible to make something like  User.Identity.UserId ?

The answer is yes, and that is why we need our CustomMembershipProvider.

Continue reading

Convert any string to a SHA 512 Hashed String

Sha 512 is a high security module, and the length of the generated string has about 130 characters.

Here is the definition for the extension.

        public static string ToHashedString(this string inputString)
        {
            var crypt = new SHA512Managed();
            //var crypt = new SHA256Managed();
            string hash = String.Empty;
            byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(inputString), 0, Encoding.UTF8.GetByteCount(inputString));
            return crypto.Aggregate(hash, (current, bit) => current + bit.ToString("x2"));
        }