This project is read-only.

Extending Windows Authentication module

Topics: Writing modules
Jul 6, 2011 at 8:10 PM

I'm trying to extend the AlexZh Windows Authentication module a little bit so that when it creates a new user, it also pulls the FirstName and LastName from our LDAP server and writes them to the FirstName and LastName fields that I've created for the Profile part using the Contrib.Profile module.

In one of my other sites, I'm gaining access to those fields in a View using

dynamic user = WorkContext.CurrentUser.ContentItem;

I can then get to the Profile fields using


Any suggestions on how to get to these fields from the other module so I can update them? I'm adding my code to the AlexZh Windows Authentication module in the method that creates a new at this point in the code, the user has already been created.

Thanks for any suggestions.

Jul 6, 2011 at 11:39 PM

Not following what's not working. Did you take a reference to that module in your own module?

Jul 7, 2011 at 1:45 AM

At this point, I'm adding my code in the AlexZh Windows Authentication module directly to see if I can get it to do what I want, then I'll worry about making my own. that module, I took a reference to Contrib.Profile...I'm just not sure how to get to the fields that I've defined for the Profile from the code file. I know how to get to the fields from a View using WorkContext...just not sure what I need to do in the class file to get to them.

Where I'm adding my code, the AlexZh Windows Authentication module has just created a new user and added it to Orchard, so there exists a user object. It doesn't seem to function like the dynamic user I referenced in my first post.

Here is the code section from the module


		public IUser GetAuthenticatedUser()
			// Try to use default authentication seervice
			var user = systemAuthenticationService.GetAuthenticatedUser();
			if (user == null)
				var httpContext = httpContextAccessor.Current();

				if (httpContext != null && httpContext.Request.IsAuthenticated && (httpContext.User.Identity is WindowsIdentity))
					// Windows authentication
					var settings = winUserSettingsService.Retrieve();
					if (!settings.EnableWindowsAuthentication)
						orchardServices.Notifier.Add(NotifyType.Error, T("Windows authentication is disabled. Please contact administrator."));
						return null;

					var windowsIdentity = (WindowsIdentity) httpContext.User.Identity;
					string userName = GetUserName(windowsIdentity);
					user = membershipService.GetUser(userName);

					// Creating new user if not exists
					if (user == null)
						string email = settings.EmailDomain;
						if (string.IsNullOrEmpty(email))
							email = null;
							email = userName + "@" + email;

						user = membershipService.CreateUser(new CreateUserParams(
						// Adding default roles to user
						if (user != null)
							var roles = settings.DefaultRoles.ToArray();

							foreach (var role in roles)
								var roleRecord = roleService.GetRoleByName(role);
								if (roleRecord != null)
									userRolesRepository.Create(new UserRolesPartRecord {Role = roleRecord, UserId = user.Id});
					// Signing In Windows User
					if (user != null)
						SignIn(user, false);
						foreach (var userEventHandler in userEventHandlers)

			return user;

Can I use the user object created in the // Creating new user if not exists section to get access to the Profile fields and update them?


Jul 7, 2011 at 11:01 PM

I don't know. What happens when you try?

Jul 8, 2011 at 7:12 PM

If I try to do the same thing for instance and set it up like this:

dynamic user2 = user.ContentItem;
user2.ProfilePart.FirstName.Value = "foo";

then it can't find ProfilePart.

My option on the user object is user.ContentItem. Under that I have user.ContentItem.ContentManager, user.ContentItem.ContentType, user.ContentItem.Parts, user.ContentItem.Record, and some others that don't really seem like they'd get me anywhere.

I thought maybe I could do something with user.ContentItem.Parts<ProfilePart>, but ProfilePart isn't recognized.

Just not sure how to get to ProfilePart for the user I just created from within this file.

Jul 8, 2011 at 7:20 PM

That would be user.As<ProfilePart>(), but are you sure it has the part?