简体   繁体   English

ASP.NET c#MD5密码登录页面

[英]ASP.NET c# MD5 Password Login page

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Site1 : System.Web.UI.MasterPage
    {


   protected void Page_Load(object sender, EventArgs e)
    {

        object User = Session["$UserName"];
        if (User != null)
        {
            pnlLogin.Visible = false;
            pnlWelcome.Visible = true;
            lblUserName.Text = User.ToString();
        }
        else
        {
            pnlWelcome.Visible = false;
            pnlLogin.Visible = true;

        }

      `protected void btnlogin_Click(object sender, EventArgs e)
    {
        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
        string sorgu = "SELECT * FROM TB_User WHERE StrUserID = @UserName AND password = @Password";

        string hashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "MD5");
        SqlCommand cmd = new SqlCommand(sorgu, cnn);


        cmd.Parameters.AddWithValue("@UserName", txtUserName);
        cmd.Parameters.AddWithValue("@Password", hashedPassword);

        cnn.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            Session.Add("User", dr["StrUserID"].ToString());
            Response.Redirect(Request.RawUrl);
        }
        else
        {
            lblresult.Text = "Login Failed";
        }

        cnn.Close();`
   }
}

here asp & html codes 这里的ASP和HTML代码

 <div class="login">
                <asp:Panel ID="pnlLogin" runat="server">
                    <div class="loghead">
                        LOGIN
                    </div>
                    <div class="logfoot">
                        <span>ID</span>
                        <br />
                        <asp:TextBox ID="txtUserName" CssClass="TextBox" runat="server" Width="151px" Height="21px" />
                        <br />
                        <span>Password</span>
                        <br />
                        <asp:TextBox ID="txtPassword" CssClass="TextBox" TextMode="Password" runat="server" Height="21px" Width="151px" />
                        <br />
                        <asp:Button ID="btnregister" CssClass="btnregister" Text="REGISTER" runat="server" OnClick="btnregpag_Click" />
                        <asp:Button ID="btnlogin" CssClass="btnlogin" Text="LOGIN" runat="server" OnClick="btnlogin_Click" />
                        <asp:Label ID="lblresult" Text="" runat="server" />
                    </div>
                </asp:Panel>
                <asp:Panel ID="pnlWelcome" runat="server">  Welcome ,<asp:Label ID="lblUserName" Text="" runat="server" /> </asp:Panel>
            </div>

Thats my code for login, database passwords crypto with md5. 那就是我的登录代码,使用md5加密数据库密码。 When i try login i got error at visual studio 2015 thats error : ERROR IMAGE HERE Whats wrong here? 当我尝试登录时,我在Visual Studio 2015上遇到了错误,那就是错误: 这里有错误图像这里怎么了? And sql table : SQL TABLE HERE 和sql表: SQL TABLE HERE

i find whats wrong txtusername after.text 我发现after.text出了什么问题txtusername

cmd.Parameters.AddWithValue("@UserName", txtUserName.text); cmd.Parameters.AddWithValue(“ @ UserName”,txtUserName.text);

Now login event working 现在登录事件正在运行

First of all there is a code syntax error in your code as mentioned in my latest comment. 首先,在我的最新评论中提到您的代码中存在代码语法错误。

Also, for md5 password storing you need to know something like 另外,对于md5密码存储,您需要了解以下内容

The output of any hash function is a collection of bytes, not a collection of text. 任何哈希函数的输出都是字节的集合,而不是文本的集合。 So when you enter text as a test you are probably entering a text conversion of that byte array. 因此,当您输入文本作为测试时,您可能正在输入该字节数组的文本转换。 Simply converting it in SQL to a binary(16) is not correct, you need to do a proper conversion, which is something you cannot do in SQL. 仅仅在SQL中将其转换为二进制(16)是不正确的,您需要进行适当的转换,而这在SQL中是无法做到的。 This also explains why changing the datatype of the column doesn't work either. 这也解释了为什么更改列的数据类型也不起作用。 Taken from here 这里取

I am presenting a small piece of code which might help you in achieving your current task. 我正在展示一小段代码,可能会帮助您完成当前任务。

Here you go:- 干得好:-

On button click 在按钮上单击

//get the username
string UserName = txtUserName.Text;

//create the MD5CryptoServiceProvider object we will use to encrypt the password
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
//create an array of bytes we will use to store the encrypted password
Byte[] hashedBytes;
//Create a UTF8Encoding object we will use to convert our password string to a byte array
UTF8Encoding encoder = new UTF8Encoding();

//encrypt the password and store it in the hashedBytes byte array
hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(txtPassword.Text));

Don't forget to add the related namespace for the hashing . 不要忘记为hashing添加相关的名称空间。 Have a look below 看看下面

using System.Security.Cryptography

For full reference you may have look at Using MD5 to Encrypt Password 有关完整参考,您可以查看使用MD5加密密码

Take a new aspx page and step into the documentation which I provided you as above. 进入一个新的aspx页面,并进入上文提供的文档。

That will surely help you to achieve your requirement. 这肯定会帮助您实现要求。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM