ASP.NET c#MD5密码登录页
这里是asp和html代码ASP.NET c#MD5密码登录页,c#,sql,asp.net,sql-server,visual-studio-2015,C#,Sql,Asp.net,Sql Server,Visual Studio 2015,这里是asp和html代码 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
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();`
}
}
这里怎么了?
和sql表:我发现了什么问题
txtusername在.text之后
cmd.Parameters.AddWithValue(“@UserName”,txtUserName.text)
现在登录事件工作了首先,正如我在最新评论中提到的,您的代码中有一个代码语法错误
另外,对于md5
密码存储,您需要了解以下内容
任何哈希函数的输出都是字节的集合,而不是文本的集合。因此,当您输入文本作为测试时,您可能正在输入该字节数组的文本转换。简单地将其在SQL中转换为二进制(16)是不正确的,您需要进行适当的转换,这是SQL中无法做到的。这也解释了为什么更改列的数据类型也不起作用。取自
我将介绍一小段代码,它可能会帮助您完成当前任务
给你:-
点击按钮
<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>
不要忘记为散列
添加相关的名称空间。看看下面
//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));
有关完整参考信息,请参阅
打开一个新的aspx页面,进入我如上所述提供给您的文档
这肯定会帮助你达到你的要求
希望对您有所帮助。您在使用md5
功能时有没有参考资料?如果是,请与ustry共享此链接:cmd.Parameters.AddWithValue(“@UserName”,@txtUserName”);cmd.Parameters.AddWithValue(“@Password”,“@hashedPassword”)@编码员:不,我没有提到md5,我应该?我确实在注册页面中使用了相同的代码,两个代码都在绿线下,但运行良好。@FurkanKalli:您需要提供更多细节。你试过什么吗?有错误吗?。正如我看到的代码一样,您还没有在公共部分类Site1:System.Web.UI.MasterPage之后关闭括号{
。所以请关闭它。感谢您经历了对密码进行散列的麻烦,因为您现在正在构建解决方案——我建议使用更合适的散列算法。MD5在这方面确实不是最好的。
using System.Security.Cryptography