【发布时间】:2018-08-08 14:36:01
【问题描述】:
我在asp.net MVC + PostgreSQL 上有一个小网络应用程序。
我有登录页面,人们在其中输入他们的登录名/密码。现在我只有 1 个用户 - admin。如果登录和密码正确,我进入mainForm.aspx页面。
但我需要创建几个用户:user 和 director。 user登录时需要重定向到user.aspx页面,director登录时需要重定向到director.aspx页面。所有情况都需要从PostgreSQL数据库进行登录/通过检查。
我该怎么做?
这是我的Login.aspx.cs 代码,只有 1 个用户:
namespace User1.WebApp
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
}
protected void Button_Login_Click(object sender, EventArgs e)
{
var connString = "Host=localhost;Username=postgres;Password=123;Database=postgres";
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
string checkuser = "select count(*) from Login where name= '" + TextBoxUserName.Text + "' ";
NpgsqlCommand com = new NpgsqlCommand(checkuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
string checkPasswordQuery = "select password from Login where name= '" + TextBoxUserName.Text + "'";
NpgsqlCommand passCom = new NpgsqlCommand(checkPasswordQuery, conn);
string password = passCom.ExecuteScalar().ToString().Replace(" ", "");
if (password == TextBoxPassword.Text)
{
Session["New"] = TextBoxUserName.Text;
Response.Redirect("MainForm.aspx");
}
else
{
Response.Write("Password is NOT correct !");
}
}
else
{
Response.Write("Username is NOT correct !");
}
}
}
}
}
【问题讨论】:
-
我在您的示例中没有看到任何尝试这样做的尝试,因此不认为您的问题对于 SO 来说是可以的,至少在没有编辑和进一步表明您尝试过的情况下是这样。这也是一个非常广泛的问题,有很多不同的方法可以实现你想要的......
-
是的,我知道...我是 C# 和编程的新手...刚开始编写我的第一个 Web 应用程序...所以我只是不知道该怎么做.
-
@badahboom ASP.NET 已经具有强加密身份验证和授权。您不需要 任何 代码。事实上,这会让你上法庭并处以 GDPR 罚款。此代码对 SQL 注入攻击开放,只需输入
' OR 1=1;--作为用户名或密码即可泄露所有用户名和密码。 -
@badahboom 不要尝试创建自己的身份验证和授权。使用内置的well documented ASP.NET 身份系统。它包含在所有 ASP.NET MVC 教程中。检查角色和权限称为
authorization。检查角色只需User.IsInRole(someRole)以检查用户的角色或GetRoles()以获取所有角色 -
@badahboom 您认为过去几年所有这些密码泄露事件是如何发生的?这可不是说笑。事实上,ASP.NET 避免这些问题准确地因为它已经提供了强身份验证。
标签: c# asp.net postgresql security model-view-controller