Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created February 26, 2013 19:36
Show Gist options
  • Save KevinJump/5041416 to your computer and use it in GitHub Desktop.
Save KevinJump/5041416 to your computer and use it in GitHub Desktop.
Form login for AD - that creates a user in Umbraco should they not exsit.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using umbraco.cms.businesslogic.member;
namespace Ldl.Umbraco.Controls.DomainLogin
{
public partial class DomainLogin : System.Web.UI.UserControl
{
public string AdUserName { get; set; }
public string AdPassword { get; set; }
public string AdDomain { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
// do the login here...
string username = Username.Text;
string password = Password.Text;
// need to do some AD checking to confirm this is the right username and password..
string ldapPath = "LDAP://DC=domain,DC=path"; // ie. LDAP://DC=foo,DC=bar,DC=com
DirectoryEntry d = new DirectoryEntry(ldapPath);
d.Username = username;
d.Password = password;
d.AuthenticationType = AuthenticationTypes.Secure;
try
{
string n = d.Name;
}
catch (Exception ex)
{
// logon failed...
Response.Write("Authentication Failed " + ex.Message);
return;
}
finally
{
d.Close();
}
string sourceDomain = "";
// if we logon then do some umbraco housekeeping...
HttpContext.Current.Trace.Write("LogonControl", "Logon User [" + username + "]");
// check to see if the user exists as a member
if (Membership.GetUser(username) == null)
{
HttpContext.Current.Trace.Write("LoginControl", "User doesn't exist - creating");
string email = string.Format("{0}@domain.local", username);
string name = username;
HttpContext.Current.Trace.Write("LoginControl", "User: " + name + " Email: " + email);
// if user doesn't exist create membership.
// we are using the default membership provider so we can maintain compatability.
// but for umbraco this only works if you set defaultMemberTypeAlias to the right thing
// in your web.config file.
// if we have some AD Credentials we could get extra info...
if (!string.IsNullOrEmpty(AdDomain) && !string.IsNullOrEmpty(AdUserName) && !string.IsNullOrEmpty(AdPassword))
{
HttpContext.Current.Trace.Write("Attempting to connect to domain [" + AdDomain + "]to get user details");
email = string.Format("{0}@{1}.local", username, AdDomain);
PrincipalContext context = new PrincipalContext(ContextType.Domain, AdDomain, AdUserName, AdPassword);
if (context != null)
{
UserPrincipal adUser = (UserPrincipal)Principal.FindByIdentity(context, username);
if (adUser != null)
{
name = string.Format("{0} {1}", adUser.GivenName, adUser.Surname);
if (String.IsNullOrEmpty(name))
name = username;
if (!string.IsNullOrEmpty(adUser.EmailAddress))
email = adUser.EmailAddress;
adUser.Dispose();
}
context.Dispose();
}
}
try
{
MembershipUser newMember = Membership.CreateUser(username, "tree4tr4", email);
Member umbracoMember = Member.GetMemberFromLoginName(username);
umbracoMember.Text = name;
umbracoMember.getProperty("sourceDomain").Value = sourceDomain;
umbracoMember.Save();
}
catch (Exception ex)
{
throw new Exception(string.Format("Username [{0}] \n {1}", username, ex.ToString()));
}
}
// Log user in automajically.
// again standard formsAuthentication so you can control all of this from the authentication
// section of web.config.
// FormsAuthentication.RedirectFromLoginPage(username, true) ;
// umbraco doesn't pass the refering path to login, it actually just renders the login page
// at the current URL, so you just redirect back to self.
if (string.IsNullOrEmpty(Request.QueryString["pauselogon"]))
{
FormsAuthentication.SetAuthCookie(username, true);
Response.Redirect(Request.Url.ToString());
// Response.Write("Redirect will go here");
}
else
{
Response.Write("Logon Paused. <a href='" + Request.Url.ToString() + "'>Carry on</a>");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment