ASP.NET到Word preSS SSO与HttpWebRequest的 [英] ASP.NET to Wordpress SSO with HttpWebRequest
问题描述
我试图创建一个asp.net网站,并使用一个简单的表单POST方法的话preSS网站之间的经验单点登录。我已经建立了使用本地字preSS功能wp_insert_user和wp_signon在MySQL数据库中创建用户帐户和登录他们一个简单的PHP页面。在我的asp.net背后创建新的用户页面code,我使用的是HttpWebRequest的POST方法来发送需要的信息到PHP页面。
它几乎作品!新词preSS用户在MySQL数据库中创建的,但他们没有登录。我怎样才能字preSS登录他们?
更新11/29/11。我已经添加了code我曾经得到这个工作。见下面
下面是我的HttpWebRequest
公用Sub LoginToWord $ P $(PSS)
这使我们的asp.net网站和字preSS之间的单点登录。
尝试
获取值
昏暗的UID作为字符串= TxtLogin.Text
DIM命令pwd作为字符串= TxtPassword.Text 格式和连接code输入数据
昏暗的编码作为新ASCIIEncoding()
昏暗POSTDATA作为字符串=(与&用户名=&放大器; UID)
POSTDATA + =(与& PWD =&放大器; PWD)
昏暗的数据以字节()= encoding.GetBytes(POSTDATA)
昏暗CC作为新的CookieContainer() 'prepare网页的请求......
昏暗myRequest由于HttpWebRequest的= WebRequest.Create(http://www.mywebsite.com/speciallogin.php)
myRequest.Method = WebRequestMethods.Http.Get
myRequest.Method =POST
myRequest.ContentType =应用/的X WWW的形式urlen codeD
myRequest.ContentLength = data.Length
myRequest.CookieContainer = CC
方通暗淡作为流= myRequest.GetRequestStream() 提交的PHP形式好友preSS注册
newStream.Write(数据,0,data.Length)
newStream.Close() 得到的回应
昏暗myResponse由于HttpWebResponse = myRequest.GetResponse()
昏暗的阅读器作为新的StreamReader(myResponse.GetResponseStream()) 看在响应cookie
如果未myResponse.Cookies.Count = 0。然后
对于每个C作为饼干在myResponse.Cookies 写这个字preSS cookie的浏览器
昏暗cookiename作为字符串= c.Name
昏暗cCookie作为新的HttpCookie(cookiename)
cCookie.Value = c.Value
cCookie.Expires = c.Expires
cCookie.Domain =.mywebsite.com
cCookie.Path =/
Response.Cookies.Add(cCookie)
下一个
万一
myResponse.Close() 抓住EX为例外
的Response.Write(前)
结束Try
结束小组
下面是PHP页面(speciallogin.php)
< PHP
包括WP-load.php'; require_once(ABSPATH WPINC'/user.php');
require_once(ABSPATH WPINC'/pluggable.php'); //从另一个页面后得到的变量
$ u_username = $ _ POST ['用户名'];
$ u_password = $ _ POST ['密码']; //建立数组
$ creds =阵列();
$ creds ['USER_LOGIN'] = $ u_username;
$ creds ['USER_PASSWORD'] = $ u_password;
$ creds ['记住'] =真; //记录用户
$ USER = wp_signon($ creds,FALSE);
如果(is_wp_error($用户))
回声$用户可> get_error_message(); //看看发生了什么
如果(is_user_logged_in()){
echo'log失败&LT'。'; BR>';
}其他{
echo'login成功<!BR>';
} wp_get_cookie_login(); 的print_r($ _ COOKIE); ?>
从我所看到的,你的服务器端code为提交请求WP-load.php这是创建和登录用户(以服务器端的Web请求的会话)。
我beleive这个词preSS将发回一个cookie每一页上,所以你必须提取了WebResponse饼干,并从asp.net页面用
的Response.Redirect(HTTP://本地主机:1350 / WP /);
我不能对此进行测试,因为我没有WP安装的那一刻,我不使用VB.net但我想它会去是这样的:
点心饼干作为新的CookieContainer()
昏暗myRequest由于HttpWebRequest的= WebRequest.Create(HTTP://本地主机:1350 / wpComm / createWPaccount.php)
myRequest.Method =POST
myRequest.ContentType =应用/的X WWW的形式urlen codeD
myRequest.ContentLength = data.Length
myRequest.CookieContainer =饼干;
方通暗淡作为流= myRequest.GetRequestStream()newStream.Write(数据,0,data.Length)
newStream.Close()对于每个C作为饼干饼干中
Response.Cookies.Add(三)
下一个的Response.Redirect(HTTP://本地主机:1350 / WP /)
I am attempting to create a single sign on experience between an asp.net site and a wordpress site using a simple form POST method. I have built a simple php page that uses the native wordpress functions wp_insert_user and wp_signon to create user account in the mysql db and sign them in. In my asp.net 'create new user' page code behind, I'm using the post method of an HttpWebRequest to send the required information to the php page.
It almost works! The new wordpress user is created in the mysql database, but they are not logged in. How can I get wordpress to log them in?
UPDATE 11/29/11. I've added the code I used to get this working. See below
Here is my 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
Here is the php page (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);
?>
From what I can see, your server side code is submitting the request to wp-load.php which is creating and logging the user in (in the session of the server side web request).
I beleive that wordpress will send back a cookie on each page, so you would have to extract the cookies from the WebResponse, and send those cookies back from your asp.net page, along with a
Response.Redirect("http://localhost:1350/wp/");
I am unable to test this as I don't have a WP install at the moment, and I don't use VB.net but i guess it would go something like:
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/")
这篇关于ASP.NET到Word preSS SSO与HttpWebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!