Wednesday, 20 July 2011

Using the LoginView Control With ASP.NET 4.0


In this tutorial we will explore ASP.NET's LoginView Control by displaying different content to users based on if they are logged in or not.

Adding the LoginView Control
At this point in the tutorial I have created a new ASP.NET Empty Web Site in Microsoft Visual Web Developer and have added in a blank Web Form. What we want to do now is add in a LoginView Control. To do this, open up your Web Form in design mode and:
  1. Expand the Login tab in your toolbox.
  2. Drag and drop a LoginView Control onto your Web Form.

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!

The changes we are going to make to this Control are going to be found in the LoginView Tasks menu. To access this, click the little arrow in the top right corner of the Control.
SS1.jpg

Adding Content to the LoginView Control
The LoginView Control contains two different views, the AnonymousTemplate which is what people who are not currently logged in see, and the LoggedInTemplate which is what people who are logged in see. To begin adding content to our LoginView:
  1. Under the LoginView Tasks menu make sure that the AnonymousTemplate view is selected.
  2. Expand the Login tab in your toolbox and drag and drop a Login Control into the editable area of the LoginView.
  3. Under the LoginView Tasks menu make sure that the LoggedInTemplate view is selected.
  4. In the editable area of the LoginView Control type in 'You are logged in!'.
What this has done is added content to the LoginView Control so that if you are not logged in you will see a Login Control and if you are logged in you will see the message 'You are logged in'. Now we need to setup our website so that we can login and test this.

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 a New Account
To enable user account creation:
  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.
Testing
Now go ahead and load up the website again. Once loaded, you will now see the AnonymousTemplate which contains the Login Control. Go ahead and login using the account you created earlier. Once logged in, notice that the content under the LoggedInTemplate is being displayed.

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The Web Form source looks like this:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LoginView ID="LoginView1" runat="server">
            <AnonymousTemplate>
                <asp:Login ID="Login1" runat="server">
                </asp:Login>
            </AnonymousTemplate>
            <LoggedInTemplate>
                You are logged in!
            </LoggedInTemplate>
            
        </asp:LoginView>
    </div>
    </form>
</body>

No comments:

Post a Comment