使用ASP.NET 2中的活动目录组登录表单
对不起,如果之前已询问过此问题,但我已搜索Google和此网站,但无法找到针对初学者的完整工作。我试图编写一个登录页面,使用ASP.NET 2对活动目录组进行身份验证。我发现了各种文章,但他们似乎都缺乏新手的关键信息。我设法拼凑了一个登录页面,它可以与几个活动目录登录一起工作,但我无法将其限制为仅限属于特定活动目录组成员的用户。 我的web.config包含以下内容:使用ASP.NET 2中的活动目录组登录表单
<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://domainname.local/DC=domainname,DC=local" />
</connectionStrings>
<authentication mode="Forms">
<forms
loginUrl="Login.aspx"
name=".ADAuthCookie" timeout="1000" />
</authentication>
<authorization>
<allow roles="DOMAINNAME\group"/>
<deny users="?"/>
</authorization>
<membership defaultProvider="MyADMembershipProvider">
<providers>
<add name="MyADMembershipProvider"
type="System.Web.Security.ActiveDirectoryMembershipProvider,
System.Web, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="ADConnectionString"
attributeMapUsername="sAMAccountName"/>
</providers>
</membership>
我有匿名的真实域名,但我相信这部分工作,它让我登陆,如果我使用:
<allow roles="DOMAINNAME\username"/>
<deny users="?"/>
的其余部分项目由具有WebControls.Login控制和在Page_Load函数以下一个Default.aspx页面一个页面的Login.aspx的证明登录已工作:
Response.Write("Hello, " + Server.HtmlEncode(User.Identity.Name));
我已经试过
<allow roles="DOMAINNAME\group"/>
<deny users="*"/>
但这似乎否认每个人。
我错过了什么?
从我可以告诉web.config的授权部分不像ActiveDirectoryMembershipProvider那样工作。看起来你需要在代码中检查角色/组的成员身份。
我花了几天最近研究你正在尝试什么,没有找到任何东西。最终实现我们自己的AD登录模块以获得所需的行为。如果您决定实施您自己的解决方案,我会建议使用ActiveDirectoryMembershipProvider进行身份验证。然后只需自己处理授权。
尝试在你的web.config文件这个变化
<configuration>
<configSections>
<section name="loginRedirectByRole" type="dirrerentloginusers.LoginRedirectByRoleSection" allowLocation="true" allowDefinition="Everywhere" />
<loginRedirectByRole>
<roleRedirects>
<add role="Manager" url="/Manager/ManagerPage.aspx" />
<add role="User" url="/User/UserPage.aspx" />
</roleRedirects>
<system.web>
<authentication>
<forms loginUrl="LoginForm1.aspx" protection="All"></forms>
</authentication>
<roleManager enabled="true"></roleManager>
<compilation debug="true" targetFramework="4.0" />
</system.web>
创建一个类logintype
public class LoginRedirectByRoleSection : ConfigurationSection
{
[ConfigurationProperty("roleRedirects")]
public RoleRedirectCollection RoleRedirects
{
get
{
return (RoleRedirectCollection)this["roleRedirects"];
}
set
{
this["roleRedirects"] = value;
}
}
}
public class RoleRedirectCollection : ConfigurationElementCollection
{
public RoleRedirect this[int index]
{
get
{
return (RoleRedirect)BaseGet(index);
}
}
public RoleRedirect this[object key]
{
get
{
return (RoleRedirect)BaseGet(key);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new RoleRedirect();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((RoleRedirect)element).Role;
}
}
public class RoleRedirect : ConfigurationElement
{
[ConfigurationProperty("role", IsRequired = true)]
public string Role
{
get
{
return (string)this["role"];
}
set
{
this["role"] = value;
}
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get
{
return (string)this["url"];
}
set
{
this["url"] = value;
}
}
}
然后在页面背后的代码添加以下代码并且将用户重定向到他的页面
private void RedirectLogin(string username)
{
LoginRedirectByRoleSection roleRedirectSection = (LoginRedirectByRoleSection)ConfigurationManager.GetSection("loginRedirectByRole");
foreach (RoleRedirect roleRedirect in roleRedirectSection.RoleRedirects)
{
if (Roles.IsUserInRole(username,roleRedirect.Role))
{
// Response.Redirect(roleRedirect.Url);
FormsAuthentication.RedirectFromLoginPage(username,true);
Response.Redirect(roleRedirect.Url);
}
}
}
我没有看到任何RoleProvider
在您发布的web.config文件。如果您想将Windows组成员身份用作ASP.NET角色,我会认为您需要WindowsTokenRoleProvider。
下面的文章将描述你在找什么? http://msdn.microsoft.com/en-us/library/ff649227.aspx –
感谢文章@ miika-l,它仍然有一些差距 - 我已经设法克服大部分但仍然不能了解如何只允许访问指定活动目录组中的用户。看起来我应该可以从web.config中的节点或通过在Application_AuthenticateRequest函数中迭代组列表来完成它,但我不知道如何执行。任何指针? 我也尝试了ASP.NET 2版本的文章http://msdn.microsoft.com/en-us/library/ff650308.aspx但有类似的问题。 –
nigelhooper
在第四步中,他们构建了用户所属的所有组的字符串。因此,您可以修改此函数以搜索您正在查找的组,或者只搜索该函数返回的字符串,就像它在doc中一样(“GetGroups方法将返回组列表作为管道分隔字符串”) 。这个方法与web.config设置没有任何关系,但是,如果你真的设置了这个方法? –