コード例 #1
0
ファイル: Login.aspx.cs プロジェクト: wudzraina/Travelmart
        /// <summary>
        /// Author:         Josephine Monteza
        /// Date Created:   12/Oct/2015
        /// Description:    Call webservice to verify if the user is authenticated through LDAP
        /// </summary>
        /// <returns></returns>
        private void AuthenticateLDAP()
        {
            string sLDAPUser      = "";
            string sLDAPID        = "";
            string sLDAPValidated = "";

            try
            {
                //if (GlobalCode.Field2String( Request.Headers["X-API-SID"]) != "")
                //{
                //    sLDAPID = GlobalCode.Field2String(Request.Headers["X-API-SID"]);
                //}
                //if (GlobalCode.Field2String(Request.Headers["X-API-User"]) != "")
                //{
                //    sLDAPUser = GlobalCode.Field2String(Request.Headers["X-API-User"]);
                //}

                if (GlobalCode.Field2String(Request.QueryString["API-SID"]) != "")
                {
                    sLDAPID = GlobalCode.Field2String(Request.QueryString["API-SID"]);
                }
                if (GlobalCode.Field2String(Request.QueryString["API-User"]) != "")
                {
                    sLDAPUser = GlobalCode.Field2String(Request.QueryString["API-User"]);
                }

                //lblLDAPUser.Text = "User: "******"SID: " + sLDAPID;

                //test
                //if (sLDAPUser != "")
                {
                    using (System.Net.WebClient client = new System.Net.WebClient())
                    {
                        string sAPI = MUser.GetLDAP();
                        client.Headers.Add("content-type", "application/json");//set your header here, you can add multiple headers

                        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
                        //verify if the username exist in LDAP
                        string sResult = client.DownloadString(sAPI + "search?user="******",{".ToCharArray());

                        string   sUserName = "";
                        string[] sUserNameArray;
                        for (int i = 0; i < sResultArray.Count(); i++)
                        {
                            if (sResultArray[i].Contains("\"uid\""))
                            {
                                sUserNameArray = sResultArray[i].Split(":".ToCharArray());
                                sUserName      = sUserNameArray[1].Replace("\"", "");
                                sUserName      = sUserName.Replace("}", "");
                            }
                        }

                        //test

                        //string sResultTest = client.DownloadString(sAPI + "search?user="******"*****@*****.**");

                        //LDAP_search testSer = new LDAP_search();
                        //testSer = Parse.JsonHelper.JsonDeserialize<LDAP_search>(sResultTest);

                        //string sResultTestRemove ="{\"status\":true,\"statuscode\":200,\"result\":{\"entries\":{\"uid\":\"[email protected]\",\"cns\":[\"travel_mart\",\"mstr\",\"ctrac_employee\"],\"rwid\":\"80223030\",\"mail\":\"[email protected]\",\"sn\":\"TMLastName\",\"givenname\":\"TM-New User 4 M TMLastName\",\"is_migrated\":\"1\"},\"found\":1,\"member\":[\"travel_mart\",\"mstr\",\"ctrac_employee\"]},\"message\":\"Lists results found.\"}";
                        //string sResultValid = sResult.Replace("\n", "");
                        //LDAP_search testSer2 = new JavaScriptSerializer().Deserialize<LDAP_search>(sResult);

                        //string jsonString = Parse.JsonHelper.JsonSerializer<LDAP_search>(testSer);



                        //verify if the sid is valid for the user
                        string sAPI_sid     = sAPI + "sid";
                        string sAPI_param   = "sid=" + sLDAPID;
                        string sSID_User    = "";
                        string sSID_Message = "";
                        string sSID_Company = "";

                        string[] sSID_MessageArray;


                        //verify if the sid is valid in LDAP
                        client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                        sResult      = client.UploadString(sAPI_sid, sAPI_param);
                        sResultArray = sResult.Split(",{".ToCharArray());

                        for (int i = 0; i < sResultArray.Count(); i++)
                        {
                            if (sResultArray[i].Contains("\"message\""))
                            {
                                sSID_MessageArray = sResultArray[i].Split(":".ToCharArray());
                                sSID_Message      = sSID_MessageArray[1].Replace("\"", "");
                                sSID_Message      = sSID_Message.Replace("}", "");
                            }

                            if (sResultArray[i].Contains("\"user\""))
                            {
                                sSID_MessageArray = sResultArray[i].Split(":".ToCharArray());
                                sSID_User         = sSID_MessageArray[1].Replace("\"", "");
                                sSID_User         = sSID_User.Replace("}", "");
                            }
                            if (sResultArray[i].Contains("\"cn\""))
                            {
                                sSID_MessageArray = sResultArray[i].Split(":".ToCharArray());
                                sSID_Company      = sSID_MessageArray[1].Replace("\"", "");
                                sSID_Company      = sSID_Company.Replace("}", "");
                            }
                        }
                        if (sSID_Message != "Session is valid.")
                        {
                            Panel2.Visible = true;
                            LabelLoginErrorDetails.Text = "LDAP Authentication: Invalid Session ID!";
                            return;
                        }
                        if (sSID_User.ToLower() != sUserName.ToLower())
                        {
                            Panel2.Visible = true;
                            LabelLoginErrorDetails.Text = "LDAP Authentication: Username is not valid!";
                            return;
                        }

                        string sAPICompany = MUser.GetLDAPCompany();
                        if (sSID_Company.ToLower() != sAPICompany.ToLower())
                        {
                            Panel2.Visible = true;
                            LabelLoginErrorDetails.Text = "LDAP Authentication: Invalid company!";
                            return;
                        }

                        sLDAPValidated = "Validated: Yes";

                        if (Membership.GetUser(sUserName) != null)
                        {
                            Login1.UserName = sUserName;
                            LoginUser();

                            UserAccountBLL.UpdateUserSessionID_LDAP(sUserName, sLDAPID);
                            FormsAuthentication.SetAuthCookie(Login1.UserName, true);
                            //FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);

                            MembershipUser mUser = Membership.GetUser(Login1.UserName);
                            DateTime       dt    = DateTime.Now;

                            mUser.LastLoginDate    = dt;
                            mUser.LastActivityDate = dt;
                            Membership.UpdateUser(mUser);

                            // lblLDAPRedirectPage.Text = "Page to redirect: LoginProcess.aspx";
                            Response.Redirect("LoginProcess.aspx", false);
                            //Response.Redirect("UnderMaintenance.html", false);
                        }
                        else
                        {
                            Panel2.Visible = true;
                            LabelLoginErrorDetails.Text = "TM Authentication: Username is not valid!";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string sMsg = "LDAP Error: " + ex.Message;
                sMsg = sMsg + "\n SID:" + sLDAPID;
                sMsg = sMsg + "\n Username:"******"\n Validated:" + sLDAPValidated;

                AlertMessage(sMsg);
            }
        }