Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Currently we are having an (ASP.NET) Order managemnet application developed using MVC3 architecture. In that to show certificates we redirect to a third party website which contains .Jsp pages. User can enter his credentials and login. This redirection is done through J query.

Now as per new requirement, user wants to login third party website with our website credentials only. I checked for cookies, input parameters on .Jsp page, but not found some needful data.

After goggling implemented this by using WebHttpRequest & WebClient classes in C#. Also used POST method.

In case of WebHttpRequest blank values are passed and login function is getting called and displaying blank field messages (though I have passed correct values.).

And in case of WebClient classes, not getting any error but unable to login.

Can anybody have implemented this anywhere ? How we can pass login parameters to .Jsp pages from Asp.Net application.

Waiting for you reply. Thanks.

You can send your reply - abhijeet.vaikar99@gmail.com

Please see below code, which I have implemented as of now.

C#
private string LoginESABUsingWebClient()
        {
            string submitButton = "Login";
            submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
            try
            {
                string userName = "wasim";
                string password = "Pass@0-0";
                
                Users user = new Users();
                user.UserName = userName;
                user.Password = password;

                string uriString = "http://test-esab-onlinelb.eu.esab.org/Home/Login";
                string postString = "userName=" + userName + "&password=" + password + "&loginButton=" + submitButton;
                Request.Cookies.Add(new HttpCookie("userCountry", "GB"));
                Request.Cookies.Add(new HttpCookie("userLanguage", "EN"));

                WebClient webClient = new WebClient();
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                webClient.Credentials = new NetworkCredential(userName, password);

                byte[] postData = Encoding.ASCII.GetBytes(postString);


                byte[] responseData = webClient.UploadData(uriString, "POST", postData);
                string srcString = Encoding.UTF8.GetString(responseData);
                return srcString;
            }
            catch (WebException ex)
            {
                return ex.Message;
            }
        }

        private string LoginThroughWebHttpRequest()
        {
            string userName = "wasim";
            string password = "Pass@0-0";

            Users user = new Users();
            user.UserName = userName;
            user.Password = password;

            //The URL of the Login Form of the website
            string urlSignin = "http://test-esab-onlinelb.eu.esab.org/Home/Login";

            ////The action URL of the Login Form of the website on Submit
            ////string urlLogin = "http://sestoplmt01:7001/ecs/POST";
            ////string urlLogin = "http://sestoplmt01:7001/ecs/document.forms[0].submit()";
            //string urlLogin = "http://sestoplmt01:7001/ecs/";

            //Initializes the Uri object of the URLs
            Uri uriSignin = new Uri(urlSignin);
            //Uri uriLogin = new Uri(urlLogin);

            //Hashtable to store the form details
            //Hashtable formData = new Hashtable();
            //formData.Add("UTC", new Hashtable());
            //formData.Add("username", new Hashtable());
            //formData.Add("password", new Hashtable());

            //((Hashtable)formData["UTC"])["value"] = -330;
            //((Hashtable)formData["username"])["value"] = username;
            //((Hashtable)formData["password"])["value"] = password;

            //Initializing the data for the post action
            String postData = "";
            postData = "userName=" + userName + "&password=" + password; 
            //foreach (string name in formData.Keys)
            //{
            //    postData += "&" + name + "=" + ((Hashtable)formData[name])["value"];
            //}
            //postData = postData.Substring(1);

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] data = encoding.GetBytes(postData);

            HttpWebRequest webReq;
            HttpWebResponse webResp;

            //To store the cookies of the response objects to be used for the next request
            CookieContainer cookies = new CookieContainer();

            String responseString = "";

            try
            {
                //Getting response for the Signin page  
                webReq = (HttpWebRequest)WebRequest.Create(urlSignin);
                webResp = (HttpWebResponse)webReq.GetResponse();

                //Storing response cookies to be used in the next request
                cookies.Add(webResp.Cookies);

                //Storing ASPSESSION cookie that appears in the Response header Set-Cookie to be used in the next request
                string sessionCookie = webResp.Headers["Set-Cookie"];
                responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();

                string respCookie = sessionCookie.Substring(0, sessionCookie.IndexOf(';'));
                char[] separator = { '=' };
                string[] cookieValues = respCookie.Split(separator);

                cookies.Add(new Cookie(cookieValues[0], cookieValues[1], "/", "test-esab-onlinelb.eu.esab.org"));
                cookies.Add(new Cookie("userCountry", "GB", "/", "test-esab-onlinelb.eu.esab.org"));
                cookies.Add(new Cookie("userLanguage", "EN", "/", "test-esab-onlinelb.eu.esab.org"));
                //Initializing the request object for log in
                webReq = (HttpWebRequest)WebRequest.Create(urlSignin);
                webReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                webReq.Referer = urlSignin;
                webReq.KeepAlive = true;
                webReq.Method = "POST";
                webReq.Credentials = new NetworkCredential(userName, password);
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.ContentLength = 0;
                webReq.AllowAutoRedirect = false;
                webReq.CookieContainer = cookies;
                webReq.Timeout = 30000;
                webReq.ReadWriteTimeout = 60000;


                //Get the response for the request to log in
                //PROBLEM OCCURS HERE - THE CODE DOES NOT EXECUTE FURTHER
                webResp = (HttpWebResponse)webReq.GetResponse();
                responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();

                webResp.Close();
                return responseString;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Posted
Updated 20-Nov-13 18:52pm
v2
Comments
keyur1991 21-Nov-13 0:43am    
Same issues that above mentioned so please reply us..
santosh2252 7-Nov-14 17:37pm    
Did you get any solution for this?
Member 14519583 3-Jul-19 5:23am    
Did anyone get a solution for this>?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900