C# 是否允许管理员以用户身份通过表单身份验证编辑页面?
我最近开始在一个以前由别人开发的网站上工作。这个网站是在asp.net上运行的,虽然我在c#方面很在行,但我对asp.net的工作原理知之甚少 我的目标是使管理员可以使用与用户编辑其详细信息时相同的工具和页面来编辑用户的详细信息。这将使这一行:C# 是否允许管理员以用户身份通过表单身份验证编辑页面?,c#,asp.net,forms-authentication,C#,Asp.net,Forms Authentication,我最近开始在一个以前由别人开发的网站上工作。这个网站是在asp.net上运行的,虽然我在c#方面很在行,但我对asp.net的工作原理知之甚少 我的目标是使管理员可以使用与用户编辑其详细信息时相同的工具和页面来编辑用户的详细信息。这将使这一行: Membership.GetUser() // returns currently logged in user 返回管理员试图编辑的常规用户,而不是返回管理员本身。如果这是可能的,这将是非常容易为我添加管理功能的网站 如果这是不可能的,我将不得不重写
Membership.GetUser() // returns currently logged in user
返回管理员试图编辑的常规用户,而不是返回管理员本身。如果这是可能的,这将是非常容易为我添加管理功能的网站
如果这是不可能的,我将不得不重写我网站上的各种功能,改为如下所示:
MembershipUser user;
if (Roles.isUserInRole("admin")) {
user = Utility.GetTheUserTheAdminIsEditing();
}
else {
user = Membership.GetUser();
}
如果我必须这样做,我应该如何实现实用程序.gettheUserTheAdministration()
目前,有如下登录功能:
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;
}
我应该如何实现这一点?我认为合适的解决方案是创建一个包含所有用户列表的管理员页面(仅对他可见),然后管理员可以对其进行编辑 您可以将此管理页面放置在站点的新文件夹中,然后在该文件夹中创建一个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>
//允许用户担任管理员角色
//拒绝其他人
甚至你也可以简单地用
角色显示\隐藏管理员链接。isUserInRole(“admin”)
谢谢你的评论。如果可能的话,我想避免创建一个单独的管理页面,因为一个用户有许多配置选项,分布在多个页面上。如果我创建了一个管理页面,我将不得不克隆所有这些功能,网站将变得更难维护。