Webmatrix 2 - Url重定向,具体取决于Razor中的用户角色

时间:2013-03-18 06:10:26

标签: javascript razor-2 webmatrix-2

在webmatrix2初创网站登录后,会出现以下链接; "hello" bob@example.com {logout}

单击该链接将转到管理部分,但是我希望它根据登录成员的角色转到自定义管理用户配置文件页面或业务配置文件页面。所以本质上代码将检查用户角色,然后根据该角色重定向到url(用户管理器)或(管理员管理器)

有没有办法在剃刀中以编程方式进行此操作。

 <section id="login">
   @if (WebSecurity.IsAuthenticated) {
     <text>Hello, <a class="email" href="~/Account/Manage"                             title="Manage">@WebSecurity.CurrentUserName</a>!

       <form id="logoutForm" action="~/Account/Logout" method="post">
         @AntiForgery.GetHtml()
           <a href="javascript:document.getElementById('logoutForm').submit()">Log out</a>
       </form>
     </text>
   } else {
           <ul>
              <li><a href="~/Account/Register-User">Register</a></li>
              <li><a href="~/Account/Login">Log in</a></li>
            </ul>
         }
       </section>

1 个答案:

答案 0 :(得分:1)

您可以使用条件块来呈现链接,具体取决于您可以使用Roles.IsUserInRole()方法确定的角色:

@if(Roles.IsUserInRole("User Manager")){
    <a href="~/ManageUser">Click</a>
}
@if(Roles.IsUserInRole("AdminManager")){
    <a href="~/ManageAdmin">Click</a>
}

或者您可以保持链接不变,并确定用户在管理页面上的角色。