ASP.NET - Cookies values are lost during traversing pages(Single-Sign on,ASP.Net and wordpress)

Asked By Nitish Kumar on 23-Dec-13 01:57 AM
I have a single sign on between Asp.Net website and wordpress blog. I am trying to make this work by using a persistent cookie which should help to bypass the login of wordpress if i am looged in asp.net.

So when i pass the values in cookie to the wordpress blog. The values are being removed by + sign. The cookie is being passed but the values are being lost and so again the user has to login in the wordpress blog.

I am stuck in this problem. Also multiple cookies are being created but none of them are having the values. All have the value "+".

ASP.NET code:
protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Retrieve and store the username and password
                String userid = Session["BUserID"].ToString();
                String pwd = Session["BPwd"].ToString();
                String Email=TxtEmail.Text;

                //Format and encode the input data
                ASCIIEncoding encoding = new ASCIIEncoding();
                String postdata = "username=" + userid;
                postdata += "&Passwd=" + pwd;
                postdata += "&Email=" + Email;
                byte[] data = encoding.GetBytes(postdata);
                CookieContainer cc = new CookieContainer();

                //Prepare the web request
                HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create("http://x.abc.com/TTY-Web/wordpress_blog/SLogin.php");
                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 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());

                //Check Cookies
                if (myResponse.Cookies.Count != 0)
                {
                    //Writing cookies in the browser
                    foreach (Cookie c in myResponse.Cookies)
                    {
                        String cookiename = c.Name;
                        System.Web.HttpCookie cCookie = new System.Web.HttpCookie(cookiename);
                        cCookie.Name = c.Name;
                        cCookie.Value = c.Value;
                        cCookie.Expires = DateTime.MinValue;
                        //cCookie.Domain = "x.abc.com";
                        cCookie.Path = "/TTY-Web/wordpress_blog/";
                        cCookie.HttpOnly = true;
                        //Response.Cookies.Remove(c.Name);
                        Response.Cookies.Add(cCookie);//Last step                                                
                        //Request.Cookies.Remove(c.Name);                        
                        //Request.Cookies.Add(cCookie);
                    }
                }                
                myResponse.Close();
                Response.Redirect("http://x.abc.com/TTY-Web/wordpress_blog/");
                
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }


Wordpress code to login the user or create if he does not exist.
<?PHP
    include 'wp-load.php';

   require_once( ABSPATH . WPINC . '/user.php' );
    require_once( ABSPATH . WPINC . '/pluggable.php' );

    //get the variables from the post of another page

$u_username = $_POST['username'];
$u_email = $_POST['Email'];
    $u_password = $_POST['Passwd'];

    //build the array
    $creds = array();
    $creds['user_login'] = $u_username;
    $creds['user_password'] = $u_password;
    $creds['remember'] = true;

//log the user in
   $user = wp_signon( $creds, true );
   if ( is_wp_error($user) ){
if( null == username_exists( $u_username ) ) {
$user_id = wp_create_user ( $u_username, $u_password, $u_email );
//echo '$user_id : '. $user_id;
$user = new WP_User( $user_id );
   $user->set_role( 'contributor' );
   $creds = array();
   $creds['user_login'] = $u_username;
   $creds['user_password'] = $u_password;
   $creds['remember'] = true;
$user = wp_signon( $creds, true );
  }
}
   
//see what happened
 /*      if ( is_user_logged_in() ) {
            echo'log in failed'.'<br>';
} else {
            echo'login success!"<br>';
}
*/
       wp_get_cookie_login() ;

       print_r($_COOKIE);
//   echo "------------\n"
  print_r($creds);

    ?>