c# - 将某些页面功能或用户界面限制为 Asp.Net 中经过身份验证的用户

标签 c# asp.net forms-authentication

我正在使用 Asp.Net/C# 构建应用程序。我正在使用 Forms Authentication。我有一个要求,我的许多 都经过身份验证( not anonymous) users 仅限于某些页面功能或用户界面。我猜 Login Control 只能用于 Authenticated vs Anonymous 用户。所以我的问题当我知道某些页面组件将对特定的经过身份验证的用户隐藏时,我该怎么做。你认为我需要在 page_load 事件上使用它来隐藏具有此类要求的页面的组件吗?

// Is this Tito visiting the page?
string userName = User.Identity.Name;
if (string.Compare(userName, "Tito", true) == 0)
 // This is Tito, SHOW the Delete column
 FilesGrid.Columns[1].Visible = true;
else
 // This is NOT Tito, HIDE the Delete column
 FilesGrid.Columns[1].Visible = false;

有没有更好的方法来完成这个。非常感谢任何帮助。谢谢

最佳答案

在这里您可以使用 Membesship User 类和 RolePrincipal将用户分开。

if(HttpContext.Current.User.IsInRole("Level1"))
{
    FilesGrid.Columns[1].Visible = true;
}
else
{
    FilesGrid.Columns[1].Visible = false;
}

因此,您为您的用户设置不同的成员(member)名称,然后根据成员(member)角色向他们展示不同的控件。

部分链接:

http://msdn.microsoft.com/en-us/library/ff648345.aspx

http://msdn.microsoft.com/en-us/library/system.web.security.roleprincipal.isinrole.aspx

关于c# - 将某些页面功能或用户界面限制为 Asp.Net 中经过身份验证的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10186549/

相关文章:

asp.net-mvc - 什么设置了 ASP.NET MVC 应用程序中的 User.Identity.IsAuthenticated?

c# - ASP.net 同时连接到两个数据库?

asp.net - 从绑定(bind)控件执行存储过程时出现“空或空全文谓词”

asp.net-web-api - 使用 MVC FormsAuthentication 在 .NET Web API 中进行身份验证

asp.net - ASP.NET 中的 Provider 是什么意思?

c# - 我可以给我的本地主机应用程序一个 'web address format'

asp.net-mvc-3 - 在MVC3中使用自定义IPrincipal和IIdentity

c# - 添加用户控件

c# - DDD,聚合根和实体

c# - C#变量UserInput找不到定义