我試圖創建一個簡單的表單POST方法之間的asp.net網站和wordpress網站之間的經驗單一登錄。我已經構建了一個簡單的PHP頁面,它使用本地wordpress函數wp_insert_user和wp_signon在mysql數據庫中創建用戶帳戶並簽名。在我的asp.net「創建新用戶」頁面代碼後面,我使用post方法的HttpWebRequest發送所需的信息到PHP頁面。ASP.NET到Wordpress單點登錄與HttpWebRequest
它幾乎可行!新的WordPress用戶是在MySQL數據庫中創建的,但他們沒有登錄。我怎樣才能讓wordpress登錄他們?
UPDATE 11/29/11。我已經添加了用於實現此功能的代碼。見下面
這裏是我的HttpWebRequest
Public Sub LoginToWordpress()
'This enables single sign on between our asp.net site and wordpress.
Try
'get the values
Dim uid As String = TxtLogin.Text
Dim pwd As String = TxtPassword.Text
'format and encode the input data
Dim encoding As New ASCIIEncoding()
Dim postData As String = ("&UserName=" & uid)
postData += ("&Pwd=" & pwd)
Dim data As Byte() = encoding.GetBytes(postData)
Dim cc As New CookieContainer()
'Prepare web request...
Dim myRequest As HttpWebRequest = WebRequest.Create("http://www.mywebsite.com/speciallogin.php")
myRequest.Method = WebRequestMethods.Http.Get
myRequest.Method = "POST"
myRequest.ContentType = "application/x-www-form-urlencoded"
myRequest.ContentLength = data.Length
myRequest.CookieContainer = cc
Dim newStream As Stream = myRequest.GetRequestStream()
'submit the php form for BuddyPress signup
newStream.Write(data, 0, data.Length)
newStream.Close()
'Get the response
Dim myResponse As HttpWebResponse = myRequest.GetResponse()
Dim reader As New StreamReader(myResponse.GetResponseStream())
'Look for cookies in the response
If Not myResponse.Cookies.Count = 0 Then
For Each c As Cookie In myResponse.Cookies
'Write the wordpress cookie to the browser
Dim cookiename As String = c.Name
Dim cCookie As New HttpCookie(cookiename)
cCookie.Value = c.Value
cCookie.Expires = c.Expires
cCookie.Domain = ".mywebsite.com"
cCookie.Path = "/"
Response.Cookies.Add(cCookie)
Next
End If
myResponse.Close()
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
這裏是PHP頁面(speciallogin.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_password = $_POST['Pwd'];
//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, false);
if (is_wp_error($user))
echo $user->get_error_message();
//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);
?>
請與分享世界,如果你設法得到這個工作:) – devrooms 2010-12-10 17:35:37
N擔心。如果我得到它的工作,我會博客,鳴叫,並從山上喊它 – Mtblewis 2010-12-10 17:58:27