从 FormsAuthentication.Authenticate 中删除用户名/密码的区分大小写

发布于 2024-09-07 21:18:54 字数 1499 浏览 5 评论 0原文

下面的代码和配置工作正常,但强制输入用户名/密码区分大小写,我想使其不区分大小写。

代码:

protected void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                string uid = UserText.Text.Trim();
                string pwd= PwdText.Text.Trim();

                if (string.IsNullOrEmpty(uid) ||
                    string.IsNullOrEmpty(pwd))
                {
                    throw new ApplicationException("UserName or Password should not be blank.");
                }

                bool isAuthrnticated = FormsAuthentication.Authenticate(uid, pwd);

                if (isAuthrnticated)
                {
                    FormsAuthentication.SetAuthCookie("Admin", false);

                    //...
                }
                else
                {
                    ((Site)this.Master).ShowError("Invalid UserName/Password.", ErrorSeverity.Warning);
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                ((Site)this.Master).ShowError(ex.Message, ErrorSeverity.Warning);
            }
        }

Web.Config

<authentication mode="Forms">
  <forms defaultUrl="~/Default.aspx" loginUrl="~/Default.aspx" slidingExpiration="true" timeout="1000">
    <credentials passwordFormat="Clear">
      <user name="Admin" password="ZAdmin"/>
    </credentials>
  </forms>
</authentication>

The below code and the config works fine, but force to enter user name/password case sensitively, i want to make it non case sensitive.

Code:

protected void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                string uid = UserText.Text.Trim();
                string pwd= PwdText.Text.Trim();

                if (string.IsNullOrEmpty(uid) ||
                    string.IsNullOrEmpty(pwd))
                {
                    throw new ApplicationException("UserName or Password should not be blank.");
                }

                bool isAuthrnticated = FormsAuthentication.Authenticate(uid, pwd);

                if (isAuthrnticated)
                {
                    FormsAuthentication.SetAuthCookie("Admin", false);

                    //...
                }
                else
                {
                    ((Site)this.Master).ShowError("Invalid UserName/Password.", ErrorSeverity.Warning);
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                ((Site)this.Master).ShowError(ex.Message, ErrorSeverity.Warning);
            }
        }

Web.Config

<authentication mode="Forms">
  <forms defaultUrl="~/Default.aspx" loginUrl="~/Default.aspx" slidingExpiration="true" timeout="1000">
    <credentials passwordFormat="Clear">
      <user name="Admin" password="ZAdmin"/>
    </credentials>
  </forms>
</authentication>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月棠 2024-09-14 21:18:54

默认情况下,用户名不区分大小写,而密码区分大小写。最简单的方法是当他们注册时,将 un 和 pw 更改为 ToUpper() 或 ToLower(),然后当您进行身份验证时,对他们输入的任何内容执行相同的操作。

string uid = UserText.Text.Trim().ToUpper();
string pwd= PwdText.Text.Trim().ToUpper();

By default, usernames are not case sensetive and passwords are. The easiest way to do this is when they register, change both un and pw to either ToUpper() or ToLower() and then when you are authenticating, do the same to whatever they enter.

string uid = UserText.Text.Trim().ToUpper();
string pwd= PwdText.Text.Trim().ToUpper();
上课铃就是安魂曲 2024-09-14 21:18:54

当您存储用户名和密码时,不要按原样存储它们,而是首先对它们调用 ToUpper() 。然后对传入 FormsAuthentication.Authenticate() 的字符串执行相同的操作。这样,在比较之前,两者都将被转换为全大写版本,从而使大小写无关。

When you store the username and password, instead of storing them as-is, call ToUpper() on them first. Then do the same thing to the strings you pass in to FormsAuthentication.Authenticate(). That way, both will have been converted to all-uppercase versions before comparing, rendering case irrelevant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文