2010-12-10 85 views
2

我試圖創建一個簡單的表單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); 

    ?> 
+0

請與分享世界,如果你設法得到這個工作:) – devrooms 2010-12-10 17:35:37

+0

N擔心。如果我得到它的工作,我會博客,鳴叫,並從山上喊它 – Mtblewis 2010-12-10 17:58:27

回答

2

從我所看到的,您的服務器端代碼提交請求wp-load.php正在創建和記錄用戶(在服務器端Web請求的會話中)。

我beleive是WordPress會發送回一個cookie每一頁上,所以你必須提取了WebResponse餅乾,並從asp.net頁面用

Response.Redirect("http://localhost:1350/wp/");

發送這些餅乾回來,一起

我不能對此進行測試,因爲我沒有WP安裝的那一刻,我不使用VB.net,但我想它會去是這樣的:

Dim cookies As New CookieContainer() 
Dim myRequest As HttpWebRequest = WebRequest.Create("http://localhost:1350/wpComm/createWPaccount.php") 
myRequest.Method = "POST" 
myRequest.ContentType = "application/x-www-form-urlencoded" 
myRequest.ContentLength = data.Length 
myRequest.CookieContainer = cookies;  
Dim newStream As Stream = myRequest.GetRequestStream() 

newStream.Write(data, 0, data.Length) 
newStream.Close() 

For Each c As Cookie In cookies 
    Response.Cookies.Add(c) 
Next 

Response.Redirect("http://localhost:1350/wp/") 
+0

原來,我需要添加一個HttpWebResponse從我的PHP形式獲取信息。但是,我運行它時,cookie數量爲0。有什麼建議麼? – Mtblewis 2010-12-10 18:32:05

+0

將需要測試它,所以我會理清虛擬機... :) – devrooms 2010-12-10 20:06:05

+0

我想通了!響應返回system.net.cookie而不是system.web.httpcookie。我使用來自另一個cookie的數據創建了一個新的httpcookie,它工作。我將記錄這些步驟,並在完成時發佈鏈接。 – Mtblewis 2010-12-10 20:52:52