Link security trimming in asp.net mvc

I would like to show some links only to authenticated users in an asp.net mvc web application.

  • I use the template for an asp.net mvc web application in Visual Studio 2008 that came with the beta release of asp.net mvc.
  • I use forms authentication.
  • I would like to add something like the following to an existing view:
<a href="/Account/ChangePassword">Change password</a>

and only show the link to users who are logged in.

What is the simplest way to do that? I would like something as simple as security trimming of the web.sitemap that I have tried with asp.net web forms. (Can that be used with mvc? Or is it only for web forms?)

Answers


The following should work. You'll also need to do something similar in the controller action for this in case the user inputs the URL by hand in their browser. Or, as you say, you could restrict access to the action in the web.config.

 <% if (HttpContext.Current.Request.IsAuthenticated) { %>
    <a href="/Account/ChangePassword">Change password</a>
 <% } %>

You could build a custom SiteMapProvider like this one:

Building an ASP.NET MVC sitemap provider with security trimming


<%if (Page.User.Identity.IsAuthenticated){ %>

show change password link

<% } else { %>

show login link

<% } %>


Need Your Help

AFNetworking 2, log out, clear session

ios objective-c session afnetworking logout

I'm using AFNetworking 2 and AFHTTPRequestOperationManager credentials for digest authentication. Everything works fine. Now I want to log out and log in with another user, But I can't clear sessio...

how to make full width when u have a container

html css header width footer

hello guys i need help im trying to make the navigation full width but its inside the div name wrapper that has set width of 900 i cant imagine how can i do it i dont want to create another div lik...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.