【问题标题】:UserPrincipal.Current returns apppool on IISUserPrincipal.Current 在 IIS 上返回 apppool
【发布时间】: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


    【解决方案1】:

    HttpContext.User 就是你想要的...

    在 ASP.NET 中,通过 Windows 身份验证进行身份验证的用户的安全上下文由 WindowsPrincipal 和 WindowsIdentity 类表示。使用 Windows 身份验证的 ASP.NET 应用程序可以通过 HttpContext.User 属性访问 WindowsPrincipal 类。

    要检索发起当前请求的 Windows 身份验证用户的安全上下文,请使用以下代码:

    使用 System.Security.Principal; ... // 获取认证用户的身份 WindowsPrincipal winPrincipal = (WindowsPrincipal)HttpContext.Current.User;

    Asp.Net Windows Auth

    【讨论】:

    • 好的。我不能使用 current(不可用),并且出现了转换错误,但 string winPrincipalName = HttpContext.User.Identity.Name; 确实给了我正确的用户。现在,我如何找出用户所属的 AD 组? findbyidentity 总是给我空结果。
    【解决方案2】:

    经过一番搜索,这对我有用

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 2012-06-29
      • 2015-07-25
      • 2021-01-18
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      相关资源
      最近更新 更多