Single Sign-on between wordpress and asp.net website
-
I have an existing asp.net website. Here i want to add a wordpress blog as a subdomain which is done without any problems. Now my requirement is to create a single sign on for both the website.
I have created a custom login page which takes the username and password and creates the user in the wordpress if he is not existing but i am unable to make him logged in when i open the wordpress blog.
For wordpress i have created a separate page slogin.php to recieve the parameters and do the task.
Following is the code foe slogin.php
<?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’] = false;$user = wp_signon( $creds, false );
if ( is_wp_error($user) ){
if( null == username_exists( $email_address ) ) {
$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’] = false;
$user = wp_signon( $creds, false );
$userID = $user->ID;wp_set_current_user( $userID, $user_login );
wp_set_auth_cookie( $userID, true, false );
do_action( ‘wp_login’, $user_login );
}
}
else
{
$userID = $user->ID;wp_set_current_user( $userID, $user_login );
wp_set_auth_cookie( $userID, true, false );
do_action( ‘wp_login’, $user_login );
}//see what happened
/* if ( is_user_logged_in() ) {
echo’log in failed’.”;
} else {
echo’login success!”‘;
}
*/
// wp_get_cookie_login() ;
//
// print_r($_COOKIE);
//// echo “————\n”
// print_r($creds);?>
The asp.net page code from where i am sending the parameters and getting the cookies in response.Following is the 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(“https://Webwordpress_blog/slogin.php”);
myrequest.Method = POST;
myrequest.ContentType = applicationx-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 = c.Expires;
cCookie.Domain = .x.abc.com;
cCookie.Path = “/”;
Response.Cookies.Add(cCookie);Last step
}
}
Response.Redirect(“https://Webwordpress_blog/”);
myResponse.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}I just dont know what to do to make the user login,the user is created succesfully but when i redirect to wordpress blog home page and perform any operation such as “New post” it again ask for Username and password. THe single-sign on does not work properly in wordpress.
- The topic ‘Single Sign-on between wordpress and asp.net website’ is closed to new replies.