Wednesday, 20 July 2011

Using the LoginStatus Control With ASP.NET 4.0


In this tutorial we will explore ASP.NET's LoginStatus Control and link it to a Login page.

Adding the LoginStatus Control
I have created a new ASP.NET Empty Web Site in Microsoft Visual Web Developer and have added in a Web Form named Default.aspx. What we want to do now is add a LoginStatus Control to the Default.aspx page, to do this open up Default.aspx to Design mode and:

Expand the Login tab in your toolbox. Drag and drop a LoginStatus Control onto your Web Form. You should now see a LoginStatus Control displaying Login.

SS1.gif

Go ahead and load up the website to see how this looks. Notice that when the page loads all you see is a Logout link. If you were to click this link right now, it would appear to do nothing. This is happening because Windows authentication is enabled by default, meaning that we are currently logged into our website with our Windows Username.

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Creating a New Account
Next, we need to create a new user account. To do this:
  1. Click the ASP.NET Configuration icon in the Solution Explorer to open up the ASP.NET Website Administration Tool.
  2. In the ASP.NET Website Administration Tool click the Security tab.
  3. Under the Users header click the Select authentication type link.
  4. Select From the internet and click Done.
  5. Under the Users header click the Create Users link.
  6. Create a new user and click Done.
  7. Close the ASP.Net Website Administration Tool.
Now go ahead and load up the website again. Notice now that the LoginStatus Control contains a link that says Login. If you click on that link right now, you will get an error because the Login.aspx page it is trying to link to does not exist.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

Creating the Login Page
Now we need to create the Login page that our LoginStatus Control will link to. Add a new Web Form to your website and name it Login.aspx. Open Login.aspx up to Design mode and:

Expand the Login tab in your toolbox. Drag and drop a Login Control onto the Web Form.

SS2.gif

Testing
Load up the website again, making sure you load the Default.aspx page where our LoginStatus Control is located. To test this out:

Click the Login link. Notice you are linked to the Login.aspx page we created. Login using the account created earlier.

SS3.gif

Notice that you are redirected back to the Default.aspx and the LoginStatus Control now displays a Logout link. Click the Logout link and notice that the LoginStatus Control displays Login again.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

The source for the Default.aspx page:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
    </div>
    </form>
</body>

The source for the Login.aspx page:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server">
        </asp:Login>
    </div>
    </form>
</body>

No comments:

Post a Comment