[英]Login form using Active directory groups in ASP.NET 2
很抱歉,如果之前已經詢問過這個問題,但是我搜索了Google和這個網站,但找不到針對初學者的完整作品。 我正在嘗試編寫一個使用ASP.NET 2對活動目錄組進行身份驗證的登錄頁面。我發現了各種文章,但它們似乎都缺乏新手的關鍵信息。 我已經設法將一個登錄頁面拼湊在一起,該登錄頁面可以處理幾個活動目錄登錄,但我不能將其限制為僅作為特定活動目錄組成員的用戶。 我的web.config包含以下內容:
<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控件的Login.aspx頁面和一個帶有以下page_load函數的Default.aspx頁面,以證明登錄已經起作用:
Response.Write("Hello, " + Server.HtmlEncode(User.Identity.Name));
我努力了
<allow roles="DOMAINNAME\group"/>
<deny users="*"/>
但這似乎否定了所有人。
我錯過了什么?
據我所知,web.config的Authorization部分不像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);
}
}
}
我在您發布的web.config文件中沒有看到任何RoleProvider
。 如果您想將Windows組成員身份用作ASP.NET角色,我原以為您需要WindowsTokenRoleProvider 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.