2013-03-18 32 views
1

在webmatrix2初學者網站中,一旦您登錄,就會出現以下鏈接; "hello" [email protected] {logout}Webmatrix2 - 根據Razor中的UserRole進行的Url重定向

點擊該鏈接將帶你到一個管理部分,但是我想它去定製取決於登錄的成員的角色管理用戶的個人資料頁或業務的個人資料頁。因此,在本質代碼將檢查用戶角色,然後根據該角色重定向到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

可以使用條件塊渲染取決於角色的鏈接,你可以通過使用Roles.IsUserInRole()方法確定:

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

或者你可以離開這個鏈接,因爲它是,並確定用戶在登錄到「管理」頁面後所處的角色。

+0

嗨邁克感謝這麼多,完美的工作,我已經添加下面更新的代碼。非常感謝丹 – daninoz 2013-03-18 23:42:58

+0

@if(WebSecurity.IsAuthenticated){ 你好, @if(Roles.IsUserInRole( 「用戶」)){ } @if(Roles.IsUserInRole( 「交易」)){ } daninoz 2013-03-18 23:46:31