C# asp.net web应用程序中的身份验证有问题
我正在尝试对从登录页面登录到我的web应用程序的用户进行身份验证。我用它作为指南,它几乎完全解释了我希望做什么,但是当我输入用户名和密码时,验证不起作用。请允许我解释一下 下面是我的HTML的相关部分。没有什么不寻常的事情:C# asp.net web应用程序中的身份验证有问题,c#,asp.net,authentication,login,authorization,C#,Asp.net,Authentication,Login,Authorization,我正在尝试对从登录页面登录到我的web应用程序的用户进行身份验证。我用它作为指南,它几乎完全解释了我希望做什么,但是当我输入用户名和密码时,验证不起作用。请允许我解释一下 下面是我的HTML的相关部分。没有什么不寻常的事情: <form id="form1" runat="server"> <div class=row> <div class=col-xs-4> <div class="form-group"> <input id="
<form id="form1" runat="server">
<div class=row>
<div class=col-xs-4>
<div class="form-group">
<input id="txtUserName" type="text" runat="server">
<ASP:RequiredFieldValidator ControlToValidate="txtUserName"
Display="Static" ErrorMessage="*" runat="server"
ID="vUserName" />
</div>
</div>
</div>
<div class=row>
<div class=col-xs-4>
<div class="form-group">
<input id="txtUserPass" type="password" runat="server">
<ASP:RequiredFieldValidator ControlToValidate="txtUserPass"
Display="Static" ErrorMessage="*" runat="server"
ID="vUserPass" />
</div>
</div>
</div>
<p><asp:Label ID="lblMsg" ForeColor="Red" runat="server" /></p>
<input type="submit" Value="Logon" runat="server" ID="cmdLogin"><p></p>
<ASP:CheckBox id="chkPersistCookie" runat="server" autopostback="false" />
</form>
现在,我已经测试了我的连接字符串,它可以工作了。它连接到SQL Server数据库中的一个表,该数据库包含3列用户、密码和用户角色。现在我在表中只有一个测试条目
但是,当我运行应用程序并在“txtUserName”中输入“test”,在“txtUserPass”中输入“password”并单击“submit”时,它将重定向回登录页面,这意味着它将为“if(ValidateUser(txtUserName.Value,txtUserPass.Value))”返回false
如果有人能帮我解决这个错误,我将不胜感激。谢谢你的帮助。:)
这是我第一次尝试进行身份验证,所以我并不完全
确定如何使用断点获取返回值
在连接到SQL server之前,您希望使用硬编码的用户名和密码进行测试
protected void cmdLogin_ServerClick(object sender, System.EventArgs e)
{
if (String.Equals(txtUserName.Value, "johndoe",
StringComparison.InvariantCultureIgnoreCase) &&
String.Equals(txtUserPass.Value, "123456",
StringComparison.InvariantCultureIgnoreCase))
{
var roles = new[] {"Administrators"};
var ticket = new FormsAuthenticationTicket(1,
txtUserName.Value,
DateTime.Now,
DateTime.Now.AddMinutes(30),
chkPersistCookie.Checked,
string.Join(",", roles),
FormsAuthentication.FormsCookiePath);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(ticket));
if (chkPersistCookie.Checked)
cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie);
string returnUrl = Request["ReturnUrl"];
if (returnUrl == null)
returnUrl = "default.aspx";
Response.Redirect(returnUrl, true);
}
else
Response.Redirect("1.0.0.0_LoginScreen.aspx", true);
}
如何创建主体对象
当向经过身份验证的用户请求页面时,您需要从cookie中检索身份验证票证,并创建一个主体对象
// Global.asax.cs
public class Global : HttpApplication
{
void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie decryptedCookie =
Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (decryptedCookie != null)
{
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(decryptedCookie.Value);
string[] roles = ticket.UserData.Split(new[] {","},
StringSplitOptions.RemoveEmptyEntries);
var identity = new GenericIdentity(ticket.Name);
var principal = new GenericPrincipal(identity, roles);
HttpContext.Current.User = principal;
Thread.CurrentPrincipal = HttpContext.Current.User;
}
}
}
用法
你在使用ASP.NET吗?@Alexander是的,我是,但不知怎么的,它被我粘贴在这里的代码删掉了。为清晰起见进行了编辑。@JaGo如果在
If(ValidateUser)设置断点(txtUse..
,返回值是什么-true还是false?如何在AuthenticateRequest中创建IPrincipal?听起来您需要学习如何使用调试器以及如何设置事件处理程序。我将查看一些IsPostBack
代码,并学习如何使用它。通过添加FormsAuthentication.SetAut,看起来非常糟糕重定向前的hCookie(userName,true)感谢您的详细响应。当我使用“johndoe”时,身份验证有效,这意味着问题一定是来自SQL Server的信息,对吗?我必须说是的。连接到SQL Server是不同的动物;很多事情很容易出错。所以,请慢慢来,先研究如何调试web应用程序。否则,你会很容易感到沮丧。祝你好运!好的,很酷。我能够用数据填充一个表从同一个数据库和同一个连接字符串在我的应用程序的其他地方,所以我猜这一定是我在“cmdLogin\u ServerClick”中做错了什么。我会调查问题所在。你帮了我很大的忙,我欠你一个,赢了。
// Global.asax.cs
public class Global : HttpApplication
{
void Application_AuthenticateRequest(object sender, EventArgs e)
{
HttpCookie decryptedCookie =
Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (decryptedCookie != null)
{
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(decryptedCookie.Value);
string[] roles = ticket.UserData.Split(new[] {","},
StringSplitOptions.RemoveEmptyEntries);
var identity = new GenericIdentity(ticket.Name);
var principal = new GenericPrincipal(identity, roles);
HttpContext.Current.User = principal;
Thread.CurrentPrincipal = HttpContext.Current.User;
}
}
}
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
string username = User.Identity.Name;
bool isAdministrator = User.IsInRole("Administrators");
}
}
}