How to implement forms authentication in asp.net c# using mssql?

liaityday

New Member
I have Login.aspx page \[code\]<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>\[/code\]and in Login.aspx.cs page i do the following\[code\]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;}\[/code\]}I have table with columns username & password with values admin & admin respectively.But Login control is giving error message Your login attempt was not successful. Please try again.Can anyone tell that what may be the problem is??
 
Back
Top