UserPrincipal.Current返回程序池在IIS [英] UserPrincipal.Current returns apppool on IIS

查看:195
本文介绍了UserPrincipal.Current返回程序池在IIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到谁是当前用户,并检查他们的小组在活动目录设置(Windows Server 2008的),看看他们是否有权限访问的MVC3网站我构建某些页面(管理员)。但是,每当我创建了一个PrincipalContext和查询当前用户,则返回该网站在其下运行的程序池。

I need to find who is the current user and check their groups in an active directory setup (windows server 2008) to see if they have permission to access certain pages (admin) on the mvc3 site I am constructing. However, whenever I create a PrincipalContext and query the current user, it returns the apppool the site is running under.

香港专业教育学院的尝试:

Ive tried:

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文件如下:

Web.config looks like:

<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>

伊夫试图实例的上下文中具有两个不同的帐户(并没有指定的帐户),他们中的一个的LDAP帐户资讯管理员用来查询。我在想什么吗?为什么总是返回程序池为当前用户?我怎样才能在用户当前已登录。

Ive tried instantiating the context with two different accounts (and with no account specified), one of them the ldap account the IT admin uses for queries. What am I missing here? Why does it always return the apppool as the current user? How can I get the current logged in user.

谢谢!

推荐答案

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

HttpContext.User is what you want...

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

In ASP.NET, the security context of a user that is authenticated with Windows authentication is represented by the WindowsPrincipal and WindowsIdentity classes. ASP.NET applications that use Windows authentication can access the WindowsPrincipal class through the HttpContext.User property.

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

To retrieve the security context of the Windows authenticated user that initiated the current request, use the following code:

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

using System.Security.Principal; ... // Obtain the authenticated user's Identity WindowsPrincipal winPrincipal = (WindowsPrincipal)HttpContext.Current.User;

Asp.Net Windows验证

这篇关于UserPrincipal.Current返回程序池在IIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆