问题
I am developing an application with masterPage.
I want to put loginStatus, LoginName controls into masterPage.
now, I want these loginStatus and LoginName controls be visible only if the user is admin. (admin will exclusively navigate to login page and no Login/logout link, logged in username should be shown for non-admins)
how can I achieve this?
回答1:
There is a LoginView, which supports roles:
<asp:LoginView ID="LoginView1" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="Admin">
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
回答2:
Need to know more about your authentication setup, but if you're using the standard ASP.NET role manager, you should be able to do something like this:
loginControl.Visible = Page.User.IsInRole("Admin"); //or whatever the role is
回答3:
check the users role
if (Page.User.IsInRole("admin"))
{
loginStatus.Visible = true;
}
来源:https://stackoverflow.com/questions/7422267/how-to-make-masterpage-contents-visible-based-on-role