我试图使用简单的表单POST方法在asp.net站点和wordpress站点之间创建单点登录体验.我已经构建了一个简单的php页面,它使用原生wordpress函数wp_insert_user和wp_signon在mysql数据库中创建用户帐户并签名.在我的asp.net’创建新用户’页面代码后面,我正在使用post方法HttpWebRequest将所需信息发送到php页面.
它几乎可以工作!新的wordpress用户是在mysql数据库中创建的,但是他们没有登录.如何让wordpress登录?
更新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);
?>
解决方法:
从我所看到的,您的服务器端代码正在向wp-load.php提交请求,该请求正在创建并记录用户(在服务器端Web请求的会话中).
我相信wordpress会在每个页面上发回一个cookie,所以你必须从WebResponse中提取cookie,然后从你的asp.net页面发回这些cookie,同时
的Response.Redirect( “HTTP://本地主机: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/")
原文地址:https://codeday.me/bug/20190930/1837210.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。