locked
Warning Error CS0618 ? RRS feed

  • Question

  • User-663551687 posted

    I have this line of code error:

    public static string EncodePassword(string Password)
            {
                string Format = "MD5";
                return FormsAuthentication.HashPasswordForStoringInConfigFile(Password, Format);//Error this line
            }

    Warning CS0618 'FormsAuthentication.HashPasswordForStoringInConfigFile (string, string)' is obsolete: 'Recommended alternative is to use Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463. '
    How is this error corrected ?

    Friday, August 23, 2019 2:55 AM

Answers

  • User-719153870 posted

    Hi dongtrien,

    dongtrien

    does not apply in my post, is it ?

    In your first post, you got a warning message, which can be fixed by the link and the little example i provided.

    How is that "does not apply in your post" ?

    Here, we will discuss the output of these different methods:

    First, the original MD5 method which you provided in the first post;

    Second, the method i provided in my post;

    Third, the one you provided in your latest post.

    Here's the demo:

    protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("Original MD5:"+EncodePassword("asd")+"<br />");
                Response.Write("GetSwcSHA1 MD5:" + GetSwcSHA1("asd")+"<br />");
                Response.Write("EncodePassword1 MD5:" + EncodePassword1("asd"));
            }
    
            public static string EncodePassword(string Password)
            {
                string Format = "MD5";
                return FormsAuthentication.HashPasswordForStoringInConfigFile(Password, Format);
            }
    
            public static string GetSwcSHA1(string value)
            {
                MD5 algorithm = MD5.Create();
                byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
                string sh1 = "";
                for (int i = 0; i < data.Length; i++)
                {
                    sh1 += data[i].ToString("x2").ToUpperInvariant();
                }
                return sh1;
            }
    
            public static string EncodePassword1(string Password)
            {
                string Format = "MD5";
                return HashString(Password, Format);
            }
    
            public static string HashString(string inputString, string hashName)
            {
                var algorithm = HashAlgorithm.Create(hashName);
                if (algorithm == null)
                    throw new ArgumentException("Unrecognized hash name", hashName);
    
                byte[] hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
                return Convert.ToBase64String(hash);
            }

    And the result of this demo:

    As you can see, my provided demo can remove the warning message AND output the same data as the original MD5 did.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2019 9:38 AM

All replies

  • User-719153870 posted

    Hi dongtrien,

    dongtrien

    Warning CS0618 'FormsAuthentication.HashPasswordForStoringInConfigFile (string, string)' is obsolete

    As the message said, this is a warning not an actual error and this API is obsolete( please check this link), which means it still can work out as you wanted to.

    To avoid the warning message, please refer to this thread, it will help you. Like below:

    public static string GetSwcSHA1(string value)
            {
                MD5 algorithm = MD5.Create();
                byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
                string sh1 = "";
                for (int i = 0; i < data.Length; i++)
                {
                    sh1 += data[i].ToString("x2").ToUpperInvariant();
                }
                return sh1;
            }

    Best Regard,

    Yang Shen

    Friday, August 23, 2019 6:44 AM
  • User-663551687 posted

    According to the document you sent me, this code 

    public static string GetSwcSHA1(string value)
            {
                MD5 algorithm = MD5.Create();
                byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
                string sh1 = "";
                for (int i = 0; i < data.Length; i++)
                {
                    sh1 += data[i].ToString("x2").ToUpperInvariant();
                }
                return sh1;
            }

    does not apply in my post, is it ? I understand that this code can replace my faulty code above, but I'm not sure if the output will produce the correct result ? have you used this code instead ?

    public static string EncodePassword(string Password)
            {            
                string Format = "MD5";            
                return HashString(Password, Format);
            }
            
            public static string HashString(string inputString, string hashName)
            {
                var algorithm = HashAlgorithm.Create(hashName);
                if (algorithm == null)
                    throw new ArgumentException("Unrecognized hash name", hashName);
    
                byte[] hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
                return Convert.ToBase64String(hash);
            }

    Monday, August 26, 2019 8:22 AM
  • User-719153870 posted

    Hi dongtrien,

    dongtrien

    does not apply in my post, is it ?

    In your first post, you got a warning message, which can be fixed by the link and the little example i provided.

    How is that "does not apply in your post" ?

    Here, we will discuss the output of these different methods:

    First, the original MD5 method which you provided in the first post;

    Second, the method i provided in my post;

    Third, the one you provided in your latest post.

    Here's the demo:

    protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("Original MD5:"+EncodePassword("asd")+"<br />");
                Response.Write("GetSwcSHA1 MD5:" + GetSwcSHA1("asd")+"<br />");
                Response.Write("EncodePassword1 MD5:" + EncodePassword1("asd"));
            }
    
            public static string EncodePassword(string Password)
            {
                string Format = "MD5";
                return FormsAuthentication.HashPasswordForStoringInConfigFile(Password, Format);
            }
    
            public static string GetSwcSHA1(string value)
            {
                MD5 algorithm = MD5.Create();
                byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
                string sh1 = "";
                for (int i = 0; i < data.Length; i++)
                {
                    sh1 += data[i].ToString("x2").ToUpperInvariant();
                }
                return sh1;
            }
    
            public static string EncodePassword1(string Password)
            {
                string Format = "MD5";
                return HashString(Password, Format);
            }
    
            public static string HashString(string inputString, string hashName)
            {
                var algorithm = HashAlgorithm.Create(hashName);
                if (algorithm == null)
                    throw new ArgumentException("Unrecognized hash name", hashName);
    
                byte[] hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
                return Convert.ToBase64String(hash);
            }

    And the result of this demo:

    As you can see, my provided demo can remove the warning message AND output the same data as the original MD5 did.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 26, 2019 9:38 AM
  • User-663551687 posted

    Thank you for answering my questions

    Wednesday, August 28, 2019 1:39 AM