从 SQL 服务器数据库获取字符串值
Get a string value from SQL Server database
我正在开发一个使用 asp.net 并使用 SQL 服务器数据库的登录系统。安全部分我还没有添加,这只是测试用的登录,暂时不关心这个。
我在数据库中有一个 Account
table,包含 username
、password
和 acc_type
列。我在登录期间想要的是,如果凭据正确,则需要检查帐户类型,并且根据帐户类型,用户将被重定向到其相应的页面。
到目前为止,这是我的代码:
protected void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=AA-PC\SQLSERVER2012;Initial Catalog=oncf;Integrated Security=True";
con.Open();
string query = "SELECT count(*) FROM Account where acc_username=@username and acc_password=@password";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
int count= Convert.ToInt32(cmd.ExecuteScalar());
if(count==1)
{
string query2 = " SELECT acc_type FROM Account where acc_username=@username and acc_password=@password";
SqlCommand cmd2 = new SqlCommand(query2, con);
cmd2.Parameters.AddWithValue("@username", TextBox1.Text);
cmd2.Parameters.AddWithValue("@password", TextBox2.Text);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(dt);
string userType = dt.Rows[0]["acc_type"].ToString();
if (userType == "LandAsset")
{
Response.Redirect("ManageLines.aspx");
}
else
{
Response.Redirect("MainAdmin.aspx");
}
}
else {
Label2.Visible = true;
}
con.Close();
}
我有一个外部 if else 条件来检查凭据是否正确,如果是这样,那么我们有逻辑检查它是哪种类型的帐户。
然而,每当我尝试登录时,无论用户是什么类型,我总是被重定向到相同的 MainAdmin.aspx
页面..
有人可以帮忙吗?
你这样做太复杂了!
基本上,如果您的用户和他的密码确实存在于 table 中,您的第二个查询将 return 他的 account_type
- 反之亦然,如果该用户没有'不存在,或者如果密码错误,则不会 returned 任何值(或 NULL
) - 只需检查一下。另外:由于您 returning 正好是一行,一列 ,您可以使用更简单的 .ExecuteScalar()
而不是填充 DataTable
和然后在其中搜索以找到您需要的信息..
所以基本上,这段代码应该做同样的事情:
protected void LoginButton_Click(object sender, EventArgs e)
{
// set up your connection string and query as strings
string connectionString = "Data Source=AA-PC\SQLSERVER2012;Initial Catalog=oncf;Integrated Security=True";
string query = "SELECT acc_type FROM dbo.Account WHERE acc_username = @username and acc_password = @password;"
// set up your connection and command in "using" blocks
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd2 = new SqlCommand(query, con))
{
// define and set parameters
cmd2.Parameters.Add("@username", SqlDbType.VarChar, 100).Value = TextBox1.Text);
cmd2.Parameters.Add("@password", SqlDbType.VarChar, 100).Value = TextBox2.Text);
// open connection, execute command, close connection
con.Open();
object result = cmd.ExecuteScalar();
con.Close();
// if nothing was returned -> user/password are not valid
if (result == null) {
Label2.Visible = true;
}
else
{
string accountType = result.ToString();
if (accountType == "LandAsset")
{
Response.Redirect("ManageLines.aspx");
}
else
{
Response.Redirect("MainAdmin.aspx");
}
}
}
}
回答你的第一个问题,如果安全是个问题,我不建议你这样做。
我发现这个系统的主要缺陷是所有经过身份验证的用户都可以轻松地在他们之间共享 link,或者只需在他们的浏览器中键入 ManageLines.aspx
并访问您的应用程序的未授权区域。
为了回答你的第二个问题 "But what is the most used way to do a login like this?",我可以推荐 asp.net 角色管理吗?
我已经成功地使用它并且效果很好。与 ASP 的许多领域一样,大部分工作已经为您完成。
这个link会给你一个总体概述:
https://msdn.microsoft.com/en-us/library/9ab2fxh0(v=vs.100).aspx
使用 aspnet_regsql.exe 实用程序在您的 SQL 服务器中配置所有必要的 table 和存储过程:
https://msdn.microsoft.com/en-us/library/ms229862(v=vs.140).aspx
然后在SQL中,在应用table中创建一条记录,为该应用添加角色,将成员添加到角色中。这一切都是在 SQL 中使用 regsql 提供的存储过程完成的。
接下来,修改您的 web.config 以包含角色提供者。然后,您可以使用存储过程将您的用户添加到角色。
<roleManager
defaultProvider="SQL"
enabled="true"
cacheRolesInCookie="true" >
</roleManager>
在您的 web.config
中,您将
而不是 <allow users="bob">
<authorization>
<allow roles="LANDASSET" />
<allow roles="ADMIN" />
<deny users="*" />
</authorization>
然后您可以创建名为 "LANDASSET" 的子文件夹和另一个名为 "ADMIN" 的子文件夹,将适当的文件放入其中,并在 web.config
中添加:
<location path="LANDASSET">
<system.web>
<authorization>
<allow roles="LANDASSET" />
<deny users="*" />
</authorization>
</system.web>
</location>
<location path="ADMIN">
<system.web>
<authorization>
<allow roles="ADMIN" />
<deny users="*" />
</authorization>
</system.web>
</location>
请注意 SQL 和您的 web.config 之间的 ROLES 和 APPLICATION 名称拼写和大小写必须全部匹配,否则它们将无法连接,即使您的连接字符串和角色提供程序是准确的。
最后,如果您仍想为您的用户提供在登录时重定向他们的便利,您可以将其添加到您的登录代码隐藏中:
确保你在顶部有这个:
using System.Web.Security;
然后在您的方法中添加:
string username = TextBox1.Text;
if (Roles.GetRolesForUser(username) == "LANDASSET")
{
Response.Redirect("ManageLines.aspx");
}
else
{
Response.Redirect("MainAdmin.aspx");
}
可能需要额外阅读一些内容才能完成,但这些是基础知识。
希望对您有所帮助!
我正在开发一个使用 asp.net 并使用 SQL 服务器数据库的登录系统。安全部分我还没有添加,这只是测试用的登录,暂时不关心这个。
我在数据库中有一个 Account
table,包含 username
、password
和 acc_type
列。我在登录期间想要的是,如果凭据正确,则需要检查帐户类型,并且根据帐户类型,用户将被重定向到其相应的页面。
到目前为止,这是我的代码:
protected void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=AA-PC\SQLSERVER2012;Initial Catalog=oncf;Integrated Security=True";
con.Open();
string query = "SELECT count(*) FROM Account where acc_username=@username and acc_password=@password";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
int count= Convert.ToInt32(cmd.ExecuteScalar());
if(count==1)
{
string query2 = " SELECT acc_type FROM Account where acc_username=@username and acc_password=@password";
SqlCommand cmd2 = new SqlCommand(query2, con);
cmd2.Parameters.AddWithValue("@username", TextBox1.Text);
cmd2.Parameters.AddWithValue("@password", TextBox2.Text);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(dt);
string userType = dt.Rows[0]["acc_type"].ToString();
if (userType == "LandAsset")
{
Response.Redirect("ManageLines.aspx");
}
else
{
Response.Redirect("MainAdmin.aspx");
}
}
else {
Label2.Visible = true;
}
con.Close();
}
我有一个外部 if else 条件来检查凭据是否正确,如果是这样,那么我们有逻辑检查它是哪种类型的帐户。
然而,每当我尝试登录时,无论用户是什么类型,我总是被重定向到相同的 MainAdmin.aspx
页面..
有人可以帮忙吗?
你这样做太复杂了!
基本上,如果您的用户和他的密码确实存在于 table 中,您的第二个查询将 return 他的 account_type
- 反之亦然,如果该用户没有'不存在,或者如果密码错误,则不会 returned 任何值(或 NULL
) - 只需检查一下。另外:由于您 returning 正好是一行,一列 ,您可以使用更简单的 .ExecuteScalar()
而不是填充 DataTable
和然后在其中搜索以找到您需要的信息..
所以基本上,这段代码应该做同样的事情:
protected void LoginButton_Click(object sender, EventArgs e)
{
// set up your connection string and query as strings
string connectionString = "Data Source=AA-PC\SQLSERVER2012;Initial Catalog=oncf;Integrated Security=True";
string query = "SELECT acc_type FROM dbo.Account WHERE acc_username = @username and acc_password = @password;"
// set up your connection and command in "using" blocks
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd2 = new SqlCommand(query, con))
{
// define and set parameters
cmd2.Parameters.Add("@username", SqlDbType.VarChar, 100).Value = TextBox1.Text);
cmd2.Parameters.Add("@password", SqlDbType.VarChar, 100).Value = TextBox2.Text);
// open connection, execute command, close connection
con.Open();
object result = cmd.ExecuteScalar();
con.Close();
// if nothing was returned -> user/password are not valid
if (result == null) {
Label2.Visible = true;
}
else
{
string accountType = result.ToString();
if (accountType == "LandAsset")
{
Response.Redirect("ManageLines.aspx");
}
else
{
Response.Redirect("MainAdmin.aspx");
}
}
}
}
回答你的第一个问题,如果安全是个问题,我不建议你这样做。
我发现这个系统的主要缺陷是所有经过身份验证的用户都可以轻松地在他们之间共享 link,或者只需在他们的浏览器中键入 ManageLines.aspx
并访问您的应用程序的未授权区域。
为了回答你的第二个问题 "But what is the most used way to do a login like this?",我可以推荐 asp.net 角色管理吗?
我已经成功地使用它并且效果很好。与 ASP 的许多领域一样,大部分工作已经为您完成。
这个link会给你一个总体概述:
https://msdn.microsoft.com/en-us/library/9ab2fxh0(v=vs.100).aspx
使用 aspnet_regsql.exe 实用程序在您的 SQL 服务器中配置所有必要的 table 和存储过程:
https://msdn.microsoft.com/en-us/library/ms229862(v=vs.140).aspx
然后在SQL中,在应用table中创建一条记录,为该应用添加角色,将成员添加到角色中。这一切都是在 SQL 中使用 regsql 提供的存储过程完成的。
接下来,修改您的 web.config 以包含角色提供者。然后,您可以使用存储过程将您的用户添加到角色。
<roleManager
defaultProvider="SQL"
enabled="true"
cacheRolesInCookie="true" >
</roleManager>
在您的 web.config
中,您将
<allow users="bob">
<authorization>
<allow roles="LANDASSET" />
<allow roles="ADMIN" />
<deny users="*" />
</authorization>
然后您可以创建名为 "LANDASSET" 的子文件夹和另一个名为 "ADMIN" 的子文件夹,将适当的文件放入其中,并在 web.config
中添加:
<location path="LANDASSET">
<system.web>
<authorization>
<allow roles="LANDASSET" />
<deny users="*" />
</authorization>
</system.web>
</location>
<location path="ADMIN">
<system.web>
<authorization>
<allow roles="ADMIN" />
<deny users="*" />
</authorization>
</system.web>
</location>
请注意 SQL 和您的 web.config 之间的 ROLES 和 APPLICATION 名称拼写和大小写必须全部匹配,否则它们将无法连接,即使您的连接字符串和角色提供程序是准确的。
最后,如果您仍想为您的用户提供在登录时重定向他们的便利,您可以将其添加到您的登录代码隐藏中:
确保你在顶部有这个:
using System.Web.Security;
然后在您的方法中添加:
string username = TextBox1.Text;
if (Roles.GetRolesForUser(username) == "LANDASSET")
{
Response.Redirect("ManageLines.aspx");
}
else
{
Response.Redirect("MainAdmin.aspx");
}
可能需要额外阅读一些内容才能完成,但这些是基础知识。
希望对您有所帮助!