Using LoginView Object

LoginView Object

If we use user accounts and/or roles on our website, it's a common task to show different content to different users.

For example, we can show different content to users who are not logged in (anonymous) and those who are logged in.

We can also enable users to see different content based on their role.

The LoginView object is an Asp.Net control that makes this job extremely easy . The LoginView object is divided into 3 main parts:

AnonymousTemplate : The content that will be shown to anonymous users, that is, not logged in, is written here.

LoggedInTemplate : The content here is shown to logged in users.

RoleGroups : This part is used to create role-specific content. In this part, an asp:RoleGroup tag is opened for each desired role, the name of the role is specified with the Roles parameter, and the ContentTemplate is opened and the content is written inside it.

Let's examine the example:

<asp:LoginView ID="LoginView1" runat="server">
 
    <AnonymousTemplate>
          <p>Please Login</p>
    </AnonymousTemplate>
 
    <LoggedInTemplate>
           <p>User Login.</p>
    </LoggedInTemplate>
 
    <RoleGroups>
        <asp:RoleGroup Roles="yonetici">
            <ContentTemplate>
                 <p>Admin Login Done . </p>
            </ContentTemplate>
        </asp:RoleGroup>
 
        <asp:RoleGroup Roles="standart">
            <ContentTemplate>
                 <p>Standard User Login.</p>
            </ContentTemplate>
        </asp:RoleGroup>
    </RoleGroups>
</asp:LoginView>

In the above example, users who are not logged in will be shown the AnonymousTemplate section, the text "Please Login". 

It is specified under the <RoleGroups> tag that different content will be displayed for users in the administrator role and different content for the users in the standard role.

Users who are not in these two roles will see the LoggedInTemplate section when they log in.

Below is Asp . Net Web Forms Site template contains sample codes used on the masterpage page.

<asp:LoginView runat="server" ViewStateMode="Disabled">
    <AnonymousTemplate>
        <ul class="nav navbar-nav navbar-right">
            <li><a runat="server" href="~/Account/Register">Sign Up</a></li>
            <li><a runat="server" href="~/Account/Login">Log In</a></li>
        </ul>
    </AnonymousTemplate>
    <LoggedInTemplate>
        <ul class="nav navbar-nav navbar-right">
            <li><a runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName()  %>!</a></li>
            <li>
                <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
            </li>
        </ul>
    </LoggedInTemplate>
</asp:LoginView>

No role-specific content is specified in this example. Sign up and Login links will appear for anonymous users.

Usernames will appear with a link to the password change page for those who log in. In addition, thanks to the LoginStatus object, the Logout button will appear and when this button is clicked, the session will be closed.

 

Show different contents to asp.net users, show different content who logged in, how to use the loginview object, show different content to asp.net roles, show different content to users in different roles

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 996 times.

Online Users: 11612



asp-net-loginview-object