2011-11-10 42 views
0

我最近開始在一個以前由其他人開發的網站上工作。該網站在asp.net上運行,雖然我非常擅長c#,但我對asp.net的工作原理並不瞭解。允許管理員以具有表單身份驗證的用戶身份編輯頁面?

我的目標是讓管理員可以使用與用戶編輯其詳細信息時相同的工具&頁來編輯用戶的詳細信息。這就是這條線:

Membership.GetUser() // returns currently logged in user 

返回管理員試圖編輯的普通用戶,而不是返回管理員本身。如果這是可能的,那麼我很容易將管理功能添加到網站。

如果無法做到這一點,我將不得不重寫我的網站上各種功能看起來像這個:

MembershipUser user; 
if (Roles.isUserInRole("admin")) { 
    user = Utility.GetTheUserTheAdminIsEditing(); 
} 
else { 
    user = Membership.GetUser(); 
} 

如果我必須這樣做,這樣,我應該如何實現Utility.GetTheUserTheAdminIsEditing()

目前,有這樣的登錄功能:

private static bool _DoLogin(String user, String pass) 
{   
    bool isAuthenticated = Membership.ValidateUser(user, pass.Trim()); 

    if (isAuthenticated == true) 
    { 
     // Create the authentication ticket 
     FormsAuthenticationTicket authTicket = new 
       FormsAuthenticationTicket(1,       // version 
             user,      // user name 
             DateTime.Now,    // creation 
             DateTime.Now.AddMinutes(60),// Expiration 
             false,      // Persistent 
             "");     // User data 


     // Now encrypt the ticket. 
     string encryptedTicket = FormsAuthentication.Encrypt(authTicket); 
     // Create a cookie and add the encrypted ticket to the 
     // cookie as data. 
     HttpCookie authCookie = 
         new HttpCookie(FormsAuthentication.FormsCookieName, 
            encryptedTicket); 


     // Add the cookie to the outgoing cookies collection. 
     HttpContext.Current.Response.Cookies.Add(authCookie); 

     FormsIdentity id = new FormsIdentity(authTicket); 
     // This principal will flow throughout the request. 
     System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, Roles.GetRolesForUser(user)); 
     // Attach the new principal object to the current HttpContext object 
     HttpContext.Current.User = principal; 

     return true; 
    } 
    else 
     return false; 
} 

我應該如何實現呢?

回答

1

我認爲合適的解決方案是用所有用戶的列表創建一個管理員頁面(只對他可見),然後管理員可以編輯它們。

你可以把這個管理頁面在一個新的文件夾在您的網站,然後創建一個文件夾中的web.config文件與該行:

<system.web> 
<authorization> 

<allow roles="Admin"/> //Allows users in Admin role 
<deny users="*"/> // deny everyone else 

</authorization> 
</system.web> 

或者你可以添加這主要web.config中

<location path="AdminFolder"> 

    <system.web> 
    <authorization> 

    <allow roles="Admin"/> //Allows users in Admin role 
    <deny users="*"/> // deny everyone else 

    </authorization> 
    </system.web> 

</location> 

甚至你可以簡單地顯示\隱藏與Roles.isUserInRole("admin"))

+0

感謝您的意見管理員聯繫。如果可能的話,我想避免製作單獨的管理頁面,因爲用戶有很多配置選項,分佈在多個頁面上。如果我創建了管理頁面,則必須克隆所有這些功能,並且該網站會變得更難以維護。 – Oliver

相關問題