ASP.NET C# Hyperlinks depending on roles
I have my Site set up with 2 different roles and then of course the anonymous one. I have a hyperlink for logged in users to go to their account page but I want the same hyperlink to be different for each role. For Example
If the role is Admin I want the Link "My Account" to go to admin/myaccount.aspx If the role is seller I want the Link "My Account" to go to seller/myaccount.aspx
I h开发者_JAVA技巧ave used a link button and plan on adding ifs to the C# but im unsure how to make an if based on the roles any idea?
Mark
Assuming Forms Authentication -
System.Security.Principal.IPrincipal _user = System.Threading.Thread.CurrentPrincipal;
if (_user.IsInRole("admin"))
{
//Set link to admin link
}
else
{
//Set to other link
}
More info on IsInRole: http://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.isinrole.aspx
Two approaches with ASP.NET, using HyperLink control:
- Use two HyperLink controls with same caption "My Account" but have different NavigateUrl property. Check user's role and dynamically set the correct link visible in Page_Load function.
- Use one HyperLink control with caption "My Account", and set the correct NavigateUrl property in Page_Load function.
精彩评论