Asp.net-ad (activedirectory) user authentication

Source: Internet
Author: User
Tags ldap

Http://blog.sina.com.cn/s/blog_5067ef490101e1ul.html

  protected void Page_Load (object sender, EventArgs e) {//Acknowledge Hishinuma start with string Staffcode = String.        Empty; if (! Checkutility.isempty (REQUEST.QUERYSTRING[CONSTVALUE.QUERY_STRING_KEY_STFCD])) {Staffcode = Request.Quer YSTRING[CONSTVALUE.QUERY_STRING_KEY_STFCD].        ToString (); } string Loginuserid = String.        Empty;            if (Checkutility.isempty (Staffcode)) {//urlパラメータで コードが obtained できなかったとき//adよりドメイン            string[] name = User.Identity.Name.Split (' \ \ ');            adよりログインユーザのドメイン Get string loginuserdomain = Name[0];            Weblogutility.writedebuglog ("ログインアカウント (ドメイン):" + Loginuserdomain);            Adよりログインユーザidを obtained loginuserid = name[1];            Weblogutility.writedebuglog ("ログインアカウント:" + Loginuserid);            LdapAuthentication LDAP = new LdapAuthentication (); Staffcode = LDAP.        GetEmployee (Loginuserdomain, Loginuserid); }//コード (AD acquisition) で Copyright Limited マスタから Copyright Limited コードを obtained dictionary<string, object> parameter = new dictionary<string, object> (); Parameter.        ADD (Staffinfobll.request_key_staff_code, Staffcode); Responsedatatype response = new STAFFINFOBLL ().        Execute (parameter);        Bllからの return but the yuan にsessionに staffinfo staffinfo = (staffinfo) response[staffinfobll.response_key_staff_info]; if (Staffinfo = = NULL | | Checkutility.isempty (STAFFINFO.STFCD)) {applexception excep = new Applexception ("Provider マスタにユーザーが exists しません.            "); Excep.            Longinuserid = Staffcode; Excep.            NTAccount = Loginuserid;        Throw excep;        } STAFFINFO.NTUSERCD = Loginuserid;        Staffinfo.ip = Getclientipaddress ();        Session[constvalue.session_key_staff_info] = Staffinfo;        Session[constvalue.session_key_sys_staff_info] = Staffinfo;        Session[constvalue.session_key_is_authenticated] = true;        } StringBuilder RedirectURL = new StringBuilder (); Dictionary<string, object> redirectkey = new dictionary<string, object> ();        Climate move first urlの get string dispid = (string) request.querystring[constvalue.query_string_key_disp_id]; Redirecturl.append (this.        GETURL (DISPID));        Redirecturl.append ("?");        Redirecturl.append (Request.QueryString.ToString ());        セッション Slew number Fujisawa session.add (Constvalue.session_key_redirect, Redirectkey);    リダイレクトresponse.redirect (Redirecturl.tostring ()); }

    /// <summary>    /// Ipアドレスの obtained     /// </summary>    /// <returns></returns>     Public string getclientipaddress ()    {        return request.servervariables["remote_addr"];    }
usingSystem;usingSystem.DirectoryServices;usingSystem.Configuration;usingOtsuka.Application.Common.Exception;usingOtsuka.Application.Common;/// <summary>///Activedirectoryのユーザ to get able/// </summary> Public classldapauthentication{/// <summary>    ///     /// </summary>    PrivateDirectoryEntry Drentry; /// <summary>    ///ドメイン name/// </summary>    Private string_domainname; /// <summary>    ///ユーザ/// </summary>    Private string_username; /// <summary>    ///パスワード/// </summary>    Private string_password; /// <summary>    ///サーバ/// </summary>    Private string_servername; //1th cited Earpick number occupies, ユーザドメイン name addition    /// <summary>    ///ユーザの belongs to するドメイン name/// </summary>    Private string_userdomainname; /// <summary>    ///コンストラクタ/// </summary>     Publicldapauthentication () {}/// <summary>    ///received Yobitsugi したディレクトリのユーザ/// </summary>    /// <param name= "DomainName" >のドメイン name of the person who acted</param>    /// <param name= "Account" >acquired したい のアカウント</param>    /// <returns>コード, the person who acted</returns>     Public stringGetEmployee (stringDomainName,stringAccount ) {         This. _domainname =Configurationmanager.appsettings[constvalue.dc_domainname].        ToString ();  This. _username =Configurationmanager.appsettings[constvalue.dc_username].        ToString ();  This. _password =Configurationmanager.appsettings[constvalue.dc_password].        ToString ();  This. _servername =Configurationmanager.appsettings[constvalue.dc_servername].                ToString (); //web.configから のドメインと unanimously するldap_dnを obtained        if(configurationmanager.appsettings["Dc_ldapdn_"+ Domainname.tolower ()]! =NULL) {_userdomainname= configurationmanager.appsettings["Dc_ldapdn_"+Domainname.tolower ()].            ToString (); _userdomainname=_userdomainname.tolower (); }        //AD Access Yobitsugi confirm        if(!Accessad ()) {            Throw NewUsernotfoundexception ("","", _username); }        //ユーザof the situationDirectorySearcher searcher =NewDirectorySearcher (); Searcher. SearchRoot=Drentry; Searcher. Filter="(Samaccountname="+ account +")"; SearchResult result=Searcher.        FindOne (); //アカウントが exists しない//アカウントのイニシャル items occupies が set されていない        if(Result = =NULL|| Result. properties["initials"]. Count.equals (0))        {            Throw NewUsernotfoundexception ("","", account); }        returnResult. properties["initials"][0].    ToString (); }    /// <summary>    ///ディレクトリへの Yobitsugi to confirm/// </summary>    /// <returns>true: Yobitsugi possible, false: Connect Yobitsugi not</returns>    Private BOOLAccessad () {//アクセスするための the Love report        stringDomainandusername = _domainname +@"\"+_username; string[] Servers = _servername.split (','); foreach(stringServerinchservers) {            stringLDAP ="ldap://"+server; //====== 2010/02/26 [clドメイン 's really into 対応] ADD START =========//にユーザドメイン The name of the outgoing word            if(!String.IsNullOrEmpty (_userdomainname)) {LDAP= LDAP +"/"+_userdomainname; }            //====== 2010/02/26 [clドメイン 's really into 対応] ADD END =========Drentry=NewDirectoryEntry (LDAP, Domainandusername, _password); Try            {                ObjectNavi =Drentry.nativeobject; return true; }            Catch            {                Continue; }        }        return false; }}

Asp.net-ad (activedirectory) user authentication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.