简体   繁体   English

asp.net mvc 中的链接安全修整

[英]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.我想在 asp.net mvc web 应用程序中仅向经过身份验证的用户显示一些链接。

  • 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.我在 Visual Studio 2008 中使用 asp.net mvc web 应用程序的模板,该应用程序随 asp.net mvc 的测试版一起提供。
  • I use forms authentication.我使用 forms 身份验证。
  • 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?)我想要像 web.sitemap 的安全修整一样简单的东西,我已经尝试过 asp.net web forms。(它可以与 mvc 一起使用吗?还是仅适用于 web forms?)

You could build a custom SiteMapProvider like this one: 你可以构建一个像这样的自定义SiteMapProvider:

Building an ASP.NET MVC sitemap provider with security trimming 使用安全修整构建ASP.NET MVC站点地​​图提供程序

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. 如果用户在浏览器中手动输入URL,您还需要在控制器操作中执行类似的操作。 Or, as you say, you could restrict access to the action in the web.config. 或者,正如您所说,您可以限制对web.config中操作的访问。

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

show change password link 显示更改密码链接

show login link 显示登录链接

You can simply it to just this: 你可以简单地说就是这样:

Change password 更改密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM