如何使用mssql在asp.net c#中实现表单身份验证?

时间:2012-09-25 06:18:48

标签: asp.net

我有Login.aspx页面

<asp:Login ID="Login1" style=" margin-top:150px;" runat="server">
    <LayoutTemplate>
        <table cellpadding="1" cellspacing="0" style=" background-color:#1b4c6c; color:White; font-family:Calibri; border-collapse:collapse;">
            <tr>
                <td>
                    <table cellpadding="0">
                        <tr>
                            <td align="center" colspan="2">
                                Log In</td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server" style=" border-radius:3px; padding-left:3px;"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                    ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                    ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="Password" runat="server" style=" border-radius:3px; padding-left:3px;" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                    ControlToValidate="Password" ErrorMessage="Password is required." 
                                    ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2" style="color:Red;">
                                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                        <tr>
                            <td align="right" colspan="2">
                                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                                    ValidationGroup="Login1" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
</asp:Login>

并在Login.aspx.cs页面中执行以下操作

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if((Session["portal"]!=null)&& (Convert.ToBoolean(Session["portal"])==true))
        {
            if (User.Identity.IsAuthenticated)
            {
                Response.Redirect("Default.aspx");
            }
        }
    }


protected static Boolean Authentication(string username, string password)
{
    string sqlstring;
             sqlstring = "Select username, password from tbluser where username='" + username + "' and password ='" + password + "'";

             // create a connection with sqldatabase 
             System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(" Data Source=./SQLEXPRESS;Initial Catalog=Database;Connect Timeout=10;TrustServerCertificate=True "); 

            // create a sql command which will user connection string and your select statement string
            System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlstring,con);

            // create a sqldatabase reader which will execute the above command to get the values from sqldatabase
            System.Data.SqlClient.SqlDataReader reader;

            // open a connection with sqldatabase
            con.Open();

            // execute sql command and store a return values in reade
            reader = comm.ExecuteReader();

            // check if reader hase any value then return true otherwise return false
            if (reader.Read())
               return true;
            else
               return false;
       }

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{

    Boolean blnresult;
    blnresult = false;
    blnresult = Authentication( Login1.UserName, Login1.Password);
    if (blnresult = true)
    {
        e.Authenticated = true;
        Session["portal"] = true;
    }
    else
        e.Authenticated = false;
}

}

我的表格中包含用户名&amp;密码值为admin&amp;管理员分别。 但是Login控件正在显示错误消息您的登录尝试未成功。请再试一次。

任何人都可以说出问题可能是什么?

0 个答案:

没有答案