Implement LDAP in ASP.net (C#) MVC4 -


i got working on asp.net application (non mvc) have change on mvc not know how adapt old code. reference, using stock website application (needed quick , dirty) , sewing in zurb's foundation framework. c# based.

here old way 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 script below (same page) worked.

<script  runat="server"> void login_click(object sender, eventargs e) {   string adpath = "ldap://dc03/dc=meowmeow,dc=com"; //path ldap directory server   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();        //create ticket, , add groups.       bool iscookiepersistent = chkpersist.checked;       formsauthenticationticket authticket = new formsauthenticationticket(1,                  txtusername.text,datetime.now, datetime.now.addminutes(60), iscookiepersistent, groups);        //encrypt ticket.       string encryptedticket = formsauthentication.encrypt(authticket);        //create cookie, , add encrypted ticket cookie data.       httpcookie authcookie = new httpcookie(formsauthentication.formscookiename, encryptedticket);        if(true == iscookiepersistent)       authcookie.expires = authticket.expiration;        //add cookie outgoing cookies collection.       response.cookies.add(authcookie);        //you can redirect now.       response.redirect(formsauthentication.getredirecturl(txtusername.text, false));     }     else     {       errorlabel.text = "authentication did not succeed. check user name , password.";     }   }   catch(exception ex)   {     errorlabel.text = "error authenticating. " + ex.message;   } } </script> 

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             {                 //bind native adsobject force authentication.                 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;                 }                  //update new path user in directory.                 _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;                  (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 following references:

~ system.directoryservices

i having extreme difficulty finding anywhere has iota of consistency in tutorial not date 2008 or so.

if can please me... have out here , needs translated, think.

i added .aspx , .cs old new, added adconnectionstring web.config, , added tokens .cs , .aspx prevent cross-site scripting (it forced me per references). can page, fill in info, when click 'submit' blanks page , nothing. still need help.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -