There are several ways to authorize asp.net .
Method using forms authentication .
To begin, let us indicate that we will use this method of authorization.
<authentication mode="Forms"> <forms name=".ASPXFORMSDEMO" loginUrl="logon.aspx" protection="All" path="/" timeout="30" /> </authentication>
Create a login page:
<h3> <font face="Verdana">Logon Page</font> </h3> <table> <tr> <td>Email:</td> <td><input id="txtUserName" type="text" runat="server"></td> <td><ASP:RequiredFieldValidator ControlToValidate="txtUserName" Display="Static" ErrorMessage="*" runat="server" ID="vUserName" /></td> </tr> <tr> <td>Password:</td> <td><input id="txtUserPass" type="password" runat="server"></td> <td><ASP:RequiredFieldValidator ControlToValidate="txtUserPass" Display="Static" ErrorMessage="*" runat="server" ID="vUserPass" /> </td> </tr> <tr> <td>Persistent Cookie:</td> <td><ASP:CheckBox id="chkPersistCookie" runat="server" autopostback="false" /></td> <td></td> </tr> </table> <input type="submit" Value="Logon" runat="server" ID="cmdLogin"><p></p> <asp:Label id="lblMsg" ForeColor="red" Font-Name="Verdana" Font-Size="10" runat="server" />
Then we hang the event handler on input.
private void cmdLogin_ServerClick(object sender, System.EventArgs e) { //Функция ValidateUser проверяет логин и пароль пользователя if (ValidateUser(txtUserName.Value,txtUserPass.Value) ) //Здесь мы записываем данные пользователя в сессию FormsAuthentication.RedirectFromLoginPage(txtUserName.Value,chkPersistCookie.Checked); else Response.Redirect("logon.aspx", true); }
Do not forget to add the namespace:
using System.Web.Security;
In order to unauthorize a user, you must perform:
FormsAuthentication.SignOut();