-
-
Save laidleymedia/9c7fc8b342fa258cadd9 to your computer and use it in GitHub Desktop.
My Login solution using MVC and querying against my LDAP server for Umbraco 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model UmbracoLogin.MemberLoginModel | |
@using (Html.BeginUmbracoForm("MemberLogin", "MemberLoginSurface")) | |
{ | |
<div class="control-group"> | |
<label class="control-label" for="username">Username<span class="required">*</span></label> | |
<div class="controls"> | |
<input id="username" type="text" name="Username" class="span3 required" data-fieldid="0"> | |
</div> | |
</div> | |
<div class="control-group"> | |
<label class="control-label" for="password">Password<span class="required">*</span></label> | |
<div class="controls"> | |
<input id="password" type="password" name="Password" class="span3 required" data-fieldid="1"> | |
</div> | |
</div> | |
<button id="submit" class="btn btn-success" type="submit" name="submit">Login</button> | |
} | |
@if (TempData["Status"] != null) | |
{ | |
<div class="row-fluid"> | |
<div class="span6 alert alert-error fade in"> | |
<button class="close" data-dismiss="alert">×</button> | |
<strong>@TempData["Status"]</strong> | |
</div> | |
</div> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web.Mvc; | |
using System.Web.Security; | |
using umbraco.cms.businesslogic.member; | |
using System.ComponentModel.DataAnnotations; | |
using System.DirectoryServices; | |
using System.DirectoryServices.AccountManagement; | |
namespace UmbracoLogin | |
{ | |
public class MemberLoginModel | |
{ | |
[Required, Display(Name = "Username")] | |
public string Username { get; set; } | |
[Required, Display(Name = "Password"), DataType(DataType.Password)] | |
public string Password { get; set; } | |
[Display(Name = "Remember me")] | |
public bool RememberMe { get; set; } | |
} | |
public class MemberLoginSurfaceController : Umbraco.Web.Mvc.SurfaceController | |
{ | |
[HttpGet] | |
[ActionName("MemberLogin")] | |
public ActionResult MemberLoginGet() | |
{ | |
return PartialView("MemberLogin", new MemberLoginModel()); | |
} | |
[HttpGet] | |
[ActionName("MemberLogout")] | |
public ActionResult MemberLogout() | |
{ | |
Session.Clear(); | |
FormsAuthentication.SignOut(); | |
return Redirect(Request.UrlReferrer.ToString()); | |
} | |
[HttpPost] | |
[ActionName("MemberLogin")] | |
public ActionResult MemberLoginPost(MemberLoginModel model, string returnUrl) | |
{ | |
if (ModelState.IsValid) | |
{ | |
// do the login here... | |
// need to do some AD checking to confirm this is the right username and password.. | |
string ldapPath = "LDAP://ldap_ip_address_here/DC=som,DC=marshall,DC=edu"; // ie. LDAP://DC=foo,DC=bar,DC=com | |
DirectoryEntry d = new DirectoryEntry(ldapPath); | |
d.Username = model.Username; | |
d.Password = model.Password; | |
d.AuthenticationType = AuthenticationTypes.Secure; | |
try | |
{ | |
string n = d.Name; | |
} | |
catch (Exception ex) | |
{ | |
// logon failed... | |
TempData["Status"] = ("Login Failed: Wrong Username/Password Combination"); | |
return Redirect(Request.UrlReferrer.ToString()); | |
} | |
finally | |
{ | |
d.Close(); | |
} | |
string sourceDomain = ""; | |
// if we logon then do some umbraco housekeeping... | |
// check to see if the user exists as a member | |
if (Membership.GetUser(model.Username) == null) | |
{ | |
string email = string.Format("{0}@marshall.edu", model.Username); | |
string name = model.Username; | |
// 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. | |
try | |
{ | |
MembershipUser newMember = Membership.CreateUser(model.Username, "tree4tr4", email); | |
Member umbracoMember = Member.GetMemberFromLoginName(model.Username); | |
umbracoMember.Text = name; | |
//umbracoMember.getProperty("sourceDomain").Value = sourceDomain; | |
umbracoMember.Save(); | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception(string.Format("Username [{0}] \n {1}", model.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(model.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>"); | |
//} | |
FormsAuthentication.SetAuthCookie(model.Username, false); | |
return Redirect(Request.UrlReferrer.ToString()); | |
} | |
TempData["Status"] = "Please enter your Username and Password to Login"; | |
return Redirect(Request.UrlReferrer.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment