24024

UserPrincipal.Current returns apppool on IIS

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;

and

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

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.

Thanks!

Answer1:

HttpContext.User is what you want...

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.

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

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

Asp.Net Windows Auth

Answer2:

This is what worked for me after some searching

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

Recommend

  • How to bind a single object instance in WPF?
  • Using DI in ConfigureService-Methods (especially IApplicationLifetime) in .NET Core
  • How do you connect to an LDAP server using node-oracledb?
  • Spring Security 3.1.3 @EnableWebSecurity
  • How to add plugin dependencies to pom.xml
  • Giving security priviliege to a scheduler in Java EE 6
  • PDF using WCF Restful Services
  • Is there some graphical way to create my own configuration file on SonarLint?
  • MYSQ & MVC3 SQL connection error \\ ProviderManifestToken but I am using MySQL
  • Authentication in Play! and RestEasy
  • Spring Cloud Microservice Architecture Confusion
  • PHP buffered output depending on server setting?
  • MVC3 Razor - ListBox pre-select not working
  • MongoError: Incorrect arguments
  • Django rest serializer Breaks when data exists
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • How can I send an e-mail from a vbs script
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • How to get a value (ex: baseURL) in every Karate feature?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Circular dependency while pushing http interceptor
  • Is there a mandatory requirement to switch app.yaml?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Why joiner is not used after Sequence generator or Update statergy
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal