

Navigator : Home > Tutorials > Controls Tutorials > ...
Using Membership & User Login controls in ASP.NET 2.0
This tutorial will show how you can set up membership and user login for a website using ASP.NET 2.0
In Visual Studio, Website > ASP.NET Configuration will take us to the Web Site Administration Tool. We need to go on the Security tab and then choose 'Use the security Setup Wizard to configure security step by step'. We will choose the options 'From the Internet', but do not need to Enable Roles for this tutorial. Choose a username, password, email and security question & answer.
Back on the Security page, chose Create Access Rules. Click on the page/directory you want to restrict access to and choose 'Anonymous Users' and 'Deny'. Then click Ok.
In order to email when a new user has been created, you can set up the SMTP server details in the Administration Tool.
From the Toolbox, you'll notice there is a whole section dedicated to Login features. You can use the LoginStatus to show whether a user is logged in or not (and provide a link to log in or log out).
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
The Login control will let a user log in to the website with their username and password.
We can also use a Validation Summary control and set its ValidationGroup to the ID of the Login control.
The LoginView control will let you specify content viewable by both logged in members and anonymous users.
We can use the LoginName control to output the username of the currently logged in user.
If we create a page in the directory that we have set an access rule for, anonymous users will not be allowed to view them. Instead, they will be asked to log in.
We can add a CreateUserWizard control to allow users to register themselves.
We can also let users change their own password with the ChangePassword control.
You can also add a PasswordRecovery control, but must have SMTP server settings correctly configured in the Adminitration Tool.
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
So with the above controls implemented, no code has been written, and yet we have a multi-function log-in system on our website.
Here is what the homepage should look like:
<form id="form1" runat="server"> <div>
<h1>Welcome to the website.<br /></h1> <br /> <asp:LoginStatus ID="LoginStatus1" runat="server" /> - <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/MemberPages/Members.aspx">Goto Members Only Page</asp:HyperLink><br /> <asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
You are logged in. Welcome, <asp:LoginName ID="LoginName1" runat="server" /> . </LoggedInTemplate> <AnonymousTemplate>
You are not logged in, please click the link above to log in. Or, please <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/Register.aspx">Register</asp:HyperLink> </AnonymousTemplate> </asp:LoginView> </div> </form> |
The Login page should look something like this:
<form id="form1" runat="server"> <div>
<asp:Login ID="Login1" runat="server" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333">
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" /> <InstructionTextStyle Font-Italic="True" ForeColor="Black" /> <TextBoxStyle Font-Size="0.8em" /> <LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" /> </asp:Login> <br /> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Login1" /> </div> </form> |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
Register.aspx:
<form id="form1" runat="server"> <div>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Home <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderStyle="Solid" BorderWidth="1px" ContinueDestinationPageUrl="~/Default.aspx" Font-Names="Verdana" Font-Size="0.8em">
<WizardSteps>
<asp:CreateUserWizardStep runat="server"> </asp:CreateUserWizardStep> <asp:CompleteWizardStep runat="server"> </asp:CompleteWizardStep> </WizardSteps> <SideBarStyle BackColor="#507CD1" Font-Size="0.9em" VerticalAlign="Top" /> <TitleTextStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <SideBarButtonStyle BackColor="#507CD1" Font-Names="Verdana" ForeColor="White" /> <NavigationButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" ForeColor="#284E98" /> <HeaderStyle BackColor="#284E98" BorderColor="#EFF3FB" BorderStyle="Solid" BorderWidth="2px" Font-Bold="True" Font-Size="0.9em" ForeColor="White" HorizontalAlign="Center" /> <CreateUserButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" ForeColor="#284E98" /> <ContinueButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" ForeColor="#284E98" /> <StepStyle Font-Size="0.8em" /> </asp:CreateUserWizard> <br /> </div> </form> |
MembersPages/ChangePassword.aspx:
<form id="form1" runat="server"> <div>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Home</asp:HyperLink> <asp:ChangePassword ID="ChangePassword1" runat="server" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" ContinueDestinationPageUrl="~/Default.aspx" Font-Names="Verdana" Font-Size="0.8em">
<CancelButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" /> <InstructionTextStyle Font-Italic="True" ForeColor="Black" /> <PasswordHintStyle Font-Italic="True" ForeColor="#507CD1" /> <ChangePasswordButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" /> <ContinueButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" /> <TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" /> <TextBoxStyle Font-Size="0.8em" /> </asp:ChangePassword> </div> </form> |
MemberPages/Members.aspx:
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
<form id="form1" runat="server"> <div>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Home</asp:HyperLink> - <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/MemberPages/ChangePassword.aspx">Change Password</asp:HyperLink> <br /> Welcome members ! This page is viewable only by logged in users. </div> </form> |
Looking for more ASP.NET Tutorials? Click Here!