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