简体   繁体   English

C#发布请求未标识为会话

[英]C# post requests not identified as session

I've been trying to have a WPF client connecting to a PHP server, logging itself in and fetching 'IsLogged.php' to validate that the client is logged in. However, 'IsLogged.php' always returns that the client isn't authenticated, what am I doing wrong? 我一直在尝试让WPF客户端连接到PHP服务器,自行登录并获取“ IsLogged.php”以验证客户端已登录。但是,“ IsLogged.php”始终返回客户端未登录的信息。身份验证,我在做什么错?

Servercode: 服务器代码:

"CreateAccount.php" “ CreateAccount.php”

session_start();

if (isset($_POST['user']))
{
    $_SESSION['UserName'] = $_POST['user'];
    echo "check";
}

"IsLogged.php" “ IsLogged.php”

session_start();

if (isset($_SESSION['UserName']))
{
    echo "allowed";
}
else
{
    echo "not allowed";
}

Client code: 客户代码:

"Post" method “发布”方法

    public static string Post(string RequestName, string PostData, out HttpStatusCode ReturnCode)
    {
        byte[] byteArray = Encoding.UTF8.GetBytes(PostData);

        WebRequest Request = WebRequest.Create(ChatAPI.Settings.BaseUrl + RequestName);
        Request.Method = "POST";
        Request.ContentType = "application/x-www-form-urlencoded";
        Request.ContentLength = byteArray.Length;

        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse response = Request.GetResponse();
        dataStream = response.GetResponseStream();

        ReturnCode = ((HttpWebResponse)response).StatusCode;

        StreamReader reader = new StreamReader(dataStream);
        string returnedData = reader.ReadToEnd();

        reader.Close();
        dataStream.Close();
        response.Close();

        return returnedData;
    }

And finally the window triggering the POST requests. 最后,窗口触发POST请求。

        HttpStatusCode Code;
        MessageBox.Show(General.Post("IsLogged", "", out Code)); --> Not allowed (intended)
        MessageBox.Show(General.Post("CreateAccount", "user=jan", out Code)); --> check (intended)
        MessageBox.Show(General.Post("IsLogged", "", out Code)); --> Not allowed (should be allowed)

Why is the server not registering the requests as a session? 服务器为什么不将请求注册为会话?

The way the server identifies the client (and the corresponding session state) is through cookies. 服务器识别客户端的方式(以及相应的会话状态)是通过cookie。

Basically, in the CreateAccount request, the server attaches a cookie to its response and expects the client to present the cookie on every subsequent request. 基本上,在CreateAccount请求中,服务器将cookie附加到其响应中,并希望客户端在每个后续请求中都显示cookie。

If the cookie is not present in a request, the server has no way of identifying the client and treats the request as coming from an unknown source. 如果请求中不存在Cookie,则服务器将无法识别客户端并将请求视为来自未知源。

Your code does not manage cookies at all, so this is why the C# client always appears to be a new client to the PHP server. 您的代码根本不管理cookie,因此这就是C#客户端始终看起来是PHP服务器的新客户端的原因。

The easiest way to save the cookie received from the server and present it on every new request is to use an instance of CookieContainer and attach it to every request you make . 保存从服务器接收的cookie并将其显示在每个新请求中的最简单方法是使用CookieContainer的实例, 并将其附加到您提出的每个请求上

I didn't try this code, so I'm not 100% sure of the syntax, but here's a starting point: 我没有尝试过这段代码,所以我不确定语法的百分百,但这是一个起点:

// this instance will be reused across multiple requests
private static CookieContainer cookieContainer = new CookieContainer();

public static string Post(string RequestName, string PostData, out HttpStatusCode ReturnCode)
{
    byte[] byteArray = Encoding.UTF8.GetBytes(PostData);

    WebRequest Request = WebRequest.Create(ChatAPI.Settings.BaseUrl + RequestName);
    Request.Method = "POST";
    Request.ContentType = "application/x-www-form-urlencoded";
    Request.ContentLength = byteArray.Length;

    Request.CookieContainer = cookieContainer; // this line is new

    Stream dataStream = Request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    WebResponse response = Request.GetResponse();
    dataStream = response.GetResponseStream();

    ReturnCode = ((HttpWebResponse)response).StatusCode;

    StreamReader reader = new StreamReader(dataStream);
    string returnedData = reader.ReadToEnd();

    reader.Close();
    dataStream.Close();
    response.Close();

    return returnedData;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM