views:

1653

answers:

3

I am having problems using a LoginView for what I need. Can somebody tell me if I can do this in a LoginView (and how) or if I need to use code-behind.

I have two roles - Administrator and User. I want to dynamically display links based on the role. I will write out what I want in an if statement because it's easier to explain:

if (role = Administrator) //Display only if administrator.
   Show Hyperlink 1
   Show Hyperlink 2
else
   if (role = User) //Display only if user.
      Show Hyperlink 3
      Show Hyperlink 4
   endif
   //Display these if a user or if non-authenticated user...
   Show Hyperlink 5
   Show Hyperlink 6
   Show Hyperlink 7 
endif
A: 

The psuedo-code looks OK, what problem are you having? Can you post the actual code?

EJB
I don't have any code yet. I wasn't sure how to go about translating my psuedo-code into my LoginView.
Mike C.
+8  A: 

Try something like this it uses the RoleGroups property. You can also specify the RoleGroup for the user, but if you use the LoggedInTemplate it will take affect for all users logged in that don't have a Group in the RoleGroup. Finally, there isn't really a way to have a shared template like the scenario you described with Users/Anonymous, so you may have to duplicate. Another possiblity is that you don't include them in your LoginView and show them to the Administrators as well.

<asp:LoginView runat="server" ID="LoginView">
    <AnonymousTemplate>
        <asp:HyperLink runat="server" ID="Link5" />
        <asp:HyperLink runat="server" ID="Link6" />
        <asp:HyperLink runat="server" ID="Link7" />
    </AnonymousTemplate>
    <LoggedInTemplate>
        <asp:HyperLink runat="server" ID="Link3" />
        <asp:HyperLink runat="server" ID="Link4" />
        <asp:HyperLink runat="server" ID="Link5" />
        <asp:HyperLink runat="server" ID="Link6" />
        <asp:HyperLink runat="server" ID="Link7" />
    </LoggedInTemplate>
    <RoleGroups>
        <asp:RoleGroup Roles="Administrator">
            <ContentTemplate>
                <asp:HyperLink runat="server" ID="Link1" />
                <asp:HyperLink runat="server" ID="Link2" />
            </ContentTemplate>
        </asp:RoleGroup>
    </RoleGroups>
</asp:LoginView>
bendewey
That's what I had in mind first, but I wanted to see if there was a way I didn't have to duplicate links 5-7. Right now it's only handful of links but I could see it grow.
Mike C.
I added an intro about that. Whats you reasoning for not wanting to duplicate the links? Any other solution I can think of isn't really adding much benefit. Atleast that's easier than just duplicating the links.
bendewey
My reason for not wanting to duplicate the links is because I don't want to have to maintain them in multiple places.
Mike C.
Is your LoginView on a MasterPage? Thats a good way to reduce the duplication. As far as the LoginView, I think that duplicating a hyperlink control is trival compared to the other techniques that I can think of.
bendewey
Yes it is on a Master Page. I agree that duplicating a hyperlink is trivial. I was just hoping there was an easy way around it.
Mike C.
+3  A: 

You can certainly achieve what you're looking for with the ASP.NET LoginView control.

You'll want to look into the "RoleGroups" property of the LoginView control, as this allows you to create ContentTemplates that differ based upon the role that the authenticated user belongs to.

For Example:

<form id="form1" runat="server">
   <asp:LoginView id="LoginView1" runat="server">
      <RoleGroups>
         <asp:RoleGroup Roles="administrator">
            <ContentTemplate>
               <ul>
                  <li>Hyperlink 1</li>
                  <li>Hyperlink 2</li>
               </ul>
            </ContentTemplate>
         </asp:RoleGroup>
         <asp:RoleGroup Roles="user">
            <ContentTemplate>
               <ul>
                  <li>Hyperlink 3</li>
                  <li>Hyperlink 4</li>
               </ul>
            </ContentTemplate>
          </asp:RoleGroup>
      </RoleGroups>
      <AnonymousTemplate>
         <ul>
            <li>Hyperlink 5</li>
            <li>Hyperlink 6</li>
         </ul>
      </AnonymousTemplate>
   </asp:LoginView>
</form>

Using this mechanism allows you to define your content for each role purely declaratively in the ASP mark-up. Alternatively, you can always do this in server-side code, and can then show/hide any controls (that runat="server") depending upon any criteria you like. For example, as an alternative to the LoginView control, you could use the MultiView/View controls.

CraigTP
In your code sample. will either the administrator or user role see Hyperlink 5 or 6?
Mike C.
I believe from my previous testing that the links will have to be duplicated in the different templates.
bendewey
OK. I'm assuming the MultiView is a container control where I can no longer directly access the controls inside a view without using the FindControl() method?
Mike C.
@Mike - In my example, links 5 and 6 will NOT be seen by anyone who is authenticated on this site (irrespective of the role(s) they may be in) since this is defined in the Anonymous template and is only seen by not-logged-in users.
CraigTP
@Mike - Re:Multiview. The MultiView/View control acts as a "sort of" container, however, think of it more as multiple sets of mark-up that are displayed conditionally. Ony one view is visible at anytime, but the controls within that view can be accessed normally via their ID properties, rather
CraigTP
than having to use the FindControl mechanism!
CraigTP
Nice. Thanks for the info about the MultiView. That's something to remember!
Mike C.