簡體   English   中英

Asp.net 身份驗證錯誤未登錄用戶

[英]Asp.net Authentication Error not logging in the user

我有我的登錄表單,我嘗試參數化登錄用戶的查詢。 但它似乎給出了用戶登錄失敗的錯誤。 我的代碼在這里。 請告訴我我在這里做錯了什么。

public void LoginUser()
{
    string UserNameFromHTML = Page.Request.Form["UserNameIput"];
    string UserPasswordFromHTML = Page.Request.Form["UserPasswordInput"];
    string QueryString = "SELECT User_Id, User_Name, User_Password FROM um_Personnel WHERE User_Name = @UserName and User_Password = @UserPassword";
    SqlCommand Command = new SqlCommand();
    Command.CommandText = QueryString;
    Command.Connection = ConnectionString;
    Command.Parameters.AddWithValue("@UserName", UserNameFromHTML);
    Command.Parameters.AddWithValue("@UserPassword", UserPasswordFromHTML);
    using (SqlDataAdapter Data_Adapter = new SqlDataAdapter(Command))
    {
        DataSet Data_Set = new DataSet();
        Data_Adapter.Fill(Data_Set);
        if (Data_Set.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("CMS/Dashboard.aspx");
        }
    }
}

我在我的按鈕onClick事件上調用這個函數作為

<button type="submit" class="submit" onclick='<% LoginUser(); %>'>

完整的 HTML 代碼在這里

<fieldset>
            <legend class="legend">User Login</legend>
            <div class="input">
                <input type="text" placeholder="Enter User Name" id="UserNameIput" required />
                <span><i class="fa fa-envelope-o"></i></span>
            </div>
            <div class="input">
                <input type="password" placeholder="Enter Password" id="UserPasswordInput" required />
                <span><i class="fa fa-lock"></i></span>
            </div>
            <button type="submit" class="submit" onclick='<% LoginUser(); %>'><i class="fa fa-long-arrow-right"></i></button>
        </fieldset>

這部分代碼

<input type="text" placeholder="Enter User Name" id="UserNameIput" required />

不包含返回參數的name屬性。 要使其工作,請添加name (我所看到的未使用該id ),並將其設為:

<input type="text" placeholder="Enter User Name" name="UserNameIput" required />

那么你可以從后面的代碼中獲取輸入:

Page.Request.Form["UserNameIput"];

對所有需要獲取隱藏代碼值或使用服務器控件的輸入執行相同操作。 我問你是否一步一步調試你的代碼,但顯然你沒有這樣做,也沒有檢查你是否有任何關於該值的內容。 所以你可能有更多的錯誤。

逐步調試您的代碼並檢查參數,檢查 sql 是否正確運行 - 並改進您的代碼。

檢查用戶是否具有正確的登錄名和密碼后,您需要實際Login用戶。

public void LoginUser()
{
    string UserNameFromHTML = Page.Request.Form["UserNameIput"];
    string UserPasswordFromHTML = Page.Request.Form["UserPasswordInput"];
    string QueryString = "SELECT User_Id, User_Name, User_Password FROM um_Personnel WHERE User_Name = @UserName and User_Password = @UserPassword";
    SqlCommand Command = new SqlCommand();
    Command.CommandText = QueryString;
    Command.Connection = ConnectionString;
    Command.Parameters.AddWithValue("@UserName", UserNameFromHTML);
    Command.Parameters.AddWithValue("@UserPassword", UserPasswordFromHTML);
    using (SqlDataAdapter Data_Adapter = new SqlDataAdapter(Command))
    {
        DataSet Data_Set = new DataSet();
        Data_Adapter.Fill(Data_Set);
        if (Data_Set.Tables[0].Rows.Count > 0)
        {
            FormsAuthentication.RedirectFromLoginPage(UserNameFromHTML, true); //will return the user to the page who needs a user who is logged in            
        }
        else
        {
            Responce.Redirect("~/Home/Index/");
        }
    }
}

Web.config添加:

<authentication mode="Forms">
    <forms name=".ASPXFORMSDEMO" loginUrl="logon.aspx" protection="All" path="/" timeout="30" />
</authentication>

您可以查看頁面以了解基本的開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM