2023-02-16 325
如何在没有登录控制的情况下使用表单身份验证.我不想在网站中使用ASP.NET登录控件.但是我必须实现表单身份验证并在数据库中验证用户.
我假设您使用的是一些文本框(例如用户名/密码)和登录按钮,而不是使用登录控件.代码看起来像这样:
在您的aspx文件中
<asp:Textbox runat="server" ID="Username"/>
<asp:Textbox runat="server" ID="Password"/>
<asp:Button runat="server" ID="Login" OnClick="Login_OnClick"/>
<asp:Label runat="server" ID="Msg" >
和服务器端:
public void Login_OnClick(object sender, EventArgs args)
{
if (Membership.ValidateUser(Username.Text,Password.Text))
{
FormsAuthentication.RedirectFromLoginPage(Username.Text,
NotPublicCheckBox.Checked);
}
else
Msg.Text = "Login failed.";
}
表格身份验证通常由2个部分组成:
有关更多信息,请查看以下文章:
<forms
loginUrl ="~/Login.aspx"
cookieless= "AutoDetect"
slidingExpiration="true"
timeout="10"
protection ="All"
/>
</authentication>
<authorization>
<deny users ="?" />
<allow users="*" />
</authorization>
loginurl->在那里使用您的登录页面,默认为login.aspx
单击您的登录按钮或登录按钮,请在验证数据库
后使用此操作.
HttpCookie authCookie = FormsAuthentication.GetAuthCookie(txtUsername.Text, false);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent,"");
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
Response.Cookies.Add(authCookie);
string redirUrl = FormsAuthentication.GetRedirectUrl(txtUsername.Text, false);
Response.Redirect(redirUrl);
在这里您可以找到有关配置web.config以符合您需求的其他信息
以上所述是小编给大家介绍的如何在没有登录控制的情况下使用表单认证?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/33985.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态