Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<pre lang="c#"> public void LoginToWordpress()
    {
    //This enables single sign on between our asp.net site and wordpress.
        try 
        {
                //get the values
                string uid = TextBox1.Text;
                string pwd = TextBox2.Text;

                Session["uid"]=TextBox1.Text;
                Session["pwd"] = TextBox2.Text;

                //format and encode the input data
                ASCIIEncoding encoding = new ASCIIEncoding();
                string postData = ("user_login=" + uid);
                postData += ("user_pass=" + pwd);
                byte[] data = encoding.GetBytes(postData);
                CookieContainer cc = new CookieContainer();

                //Prepare web request...
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://mysite.com/blog/wp-admin/");
	
                myRequest.Method = WebRequestMethods.Http.Get;
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                myRequest.CookieContainer = cc;
                Stream newStream = myRequest.GetRequestStream();

                //submit the php form for BuddyPress signup
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                //Get the response
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream());


                String adv=reader.ReadToEnd();
            
                HttpCookie aCookie,VCookie,Dcookie,PCookie;
                string cookieName,cookievalue,cookiedomain,cookiepath;
                int limit = myResponse.Cookies.Count;
                for (int i = 0; i < limit; i++)
                {
                    cookieName = myResponse.Cookies[i].Name;
                    cookievalue = myResponse.Cookies[i].Value;
                    cookiedomain = myResponse.Cookies[i].Domain;
                    cookiepath = myResponse.Cookies[i].Path;
                   

                    aCookie = new HttpCookie(cookieName);
                    VCookie = new HttpCookie(cookievalue);
                    Dcookie = new HttpCookie(cookiedomain);
                    PCookie = new HttpCookie(cookiepath);
             
                    
                    Response.Cookies.Add(aCookie);
                    Response.Cookies.Add(VCookie);
                    Response.Cookies.Add(Dcookie);
                    Response.Cookies.Add(PCookie);

                }

        }   
        catch (Exception ex) 
        {
         Response.Write(ex);
        }
    }



The code shows that I want to login from asp.net to Wordpress without logging- in again from Wordpress I should be able to see the Dashboard.

Code is working fine...Cookies are saved but wordpress is unable to fetch.

Please if anybody knows, please help


Thanks in advance
Posted

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