I originally got this working on a ASP.net application (non MVC) but now that I have to change over to MVC I do not know how to adapt my old code. For reference, I am using the stock website you get for the application (needed to be quick and dirty) and I am also sewing in Zurb's Foundation framework. This is also C# based.
Here is the old way that worked:
LOGIN.ASPX
<form id="Login" method="post" runat="server">
<fieldset>
<legend>Please login</legend>
<asp:Label ID="errorLabel" Runat=server ForeColor=#ff3300></asp:Label><br>
<div class="row">
<div class="large-12 columns">
<label>Domain:</label>
<asp:TextBox ID="txtDomain" Runat=server placeholder="Human Check: Please type WORKGROUP"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Username:</label>
<asp:TextBox ID=txtUsername Runat=server ></asp:TextBox>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Password:</label>
<asp:TextBox ID="txtPassword" Runat=server TextMode=Password></asp:TextBox><br>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<%-- <a href="#" class="button" id="btnLogin" runat="server" önclick="Login_Click">Submit</a>--%>
<asp:Button ID="Button1" Runat=server Text="Login" OnClick="Login_Click" CssClass="button"></asp:Button>
</div>
<div class="large-6 columns">
<br />
<asp:CheckBox ID=chkPersist Runat=server /> Remember Me
</div>
</div>
</fieldset>
</form>
Here was the script below (same page) that worked.
<script runat="server">
void Login_Click(object sender, EventArgs e)
{
string adPath = "LDAP://DC03/DC=Meowmeow,dc=com";
Legend_Forms_Manager.LdapAuthentication adAuth = new Legend_Forms_Manager.LdapAuthentication(adPath);
try
{
if (true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
{
string groups = adAuth.GetGroups();
bool isCookiePersistent = chkPersist.Checked;
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
txtUsername.Text,DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
if(true == isCookiePersistent)
authCookie.Expires = authTicket.Expiration;
Response.Cookies.Add(authCookie);
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
}
else
{
errorLabel.Text = "Authentication did not succeed. Check user name and password.";
}
}
catch(Exception ex)
{
errorLabel.Text = "Error authenticating. " + ex.Message;
}
}
</script>
Here was the LdapAuthentication.cs
using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;
namespace Legend_Forms_Manager
{
public class LdapAuthentication
{
private string _path;
private string _filterAttribute;
public LdapAuthentication(string path)
{
_path = path;
}
public bool IsAuthenticated(string domain, string username, string pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd, AuthenticationTypes.SecureSocketsLayer);
try
{
object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
_path = result.Path;
_filterAttribute = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
public string GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
string dn;
int equalsIndex, commaIndex;
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (string)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
}
I included the following references:
~ System.DirectoryServices
I am having EXTREME difficulty finding anywhere that has any iota of consistency in a tutorial that does not date back to 2008 or so.
If you can please help me... I have everything out here and now it just needs to be translated, I think.