加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 使用HttpWebRequest的ASP.NET到WordPress SSO

发布时间:2021-07-06 05:19:04 所属栏目:大数据 来源: https://www.jb51.cc
导读:我试图使用简单的表单POST方法在asp.net站点和wordpress站点之间创建单点登录体验.我已经构建了一个简单的PHP页面,它使用原生wordpress函数wp_insert_user和wp_signon在mysql数据库中创建用户帐户并签名.在我的asp.net’创建新用户’页面代码后面,我正在使用

我试图使用简单的表单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/")

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读