【发布时间】:2012-01-25 14:37:03
【问题描述】:
我需要找出谁是当前用户,并在活动目录设置(windows server 2008)中检查他们的组,以查看他们是否有权访问我正在构建的 mvc3 站点上的某些页面(管理员)。但是,每当我创建一个 PrincipalContext 并查询当前用户时,它都会返回该站点正在运行的 apppool。
我试过了:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal currentuser = UserPrincipal.Current;
string username = currentuser.DisplayName;
和
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "domain", "CN=dbcn LDAP,OU=the - account,DC=thedc,DC=local", "domain\\user", "password");
UserPrincipal currentuser = UserPrincipal.Current;
string username = currentuser.DisplayName;
Web.config 看起来像:
<configuration>
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>
</appSettings>
<authentication mode="Windows" />
<membership defaultProvider="AspNetActiveDirectoryMembershipProvider">
<providers>
<clear />
<add name="AspNetActiveDirectoryMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="service" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
<providers>
<clear />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
<identity impersonate="false" />
<connectionStrings>
<add name="foocontext" connectionString="data source=foo;Initial Catalog=foo;Integrated Security=SSPI;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
<add name="ADService" connectionString="LDAP://foo.local/OU=the - service,DC=foo,DC=local" />
</connectionStrings>
</configuration>
我尝试使用两个不同的帐户(并且没有指定帐户)来实例化上下文,其中一个是 IT 管理员用于查询的 ldap 帐户。我在这里想念什么?为什么它总是将 apppool 作为当前用户返回?如何获取当前登录用户。
谢谢!
【问题讨论】:
标签: asp.net-mvc-3 iis active-directory