How to login to wordpress programmatically?

前端 未结 8 1424
时光取名叫无心
时光取名叫无心 2020-12-24 04:14

I need to perform some action in wordpress admin panel programmatically but can\'t manage how to login to Wordpress using C# and HttpWebRequest.

Here is what I do:

8条回答
  •  被撕碎了的回忆
    2020-12-24 04:48

    TomerBu's answer works for me with the following additions. I had to install an SSL certificate on the website, add support for TLS1.2 and set the UserAgent in order for this to work. Without TLS1.2, the webserver immediately rejected my connection request. Without the SSL cert, the WordPress site did not consider my C# bot to be logged in for subsequent WebRequests (even though the login was successful).

    *** Important note about TLS: I'm a security protocol novice and am only providing what worked for me. I installed the .NET 4.7.2 Developer Pack and changed the target framework for my C# Project to .NET 4.7.2, but I still needed to modify the ServicePointManager.SecurityProtocol as shown below. Search to find best practices including updating .NET and specifying multiple TLS versions in a bitwise ORed statement.

    // Add support for TLS 1.2 (note bitwise OR)
    ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
    
    ...
    request.Proxy = null;
    request.AllowAutoRedirect = false;
    request.CookieContainer = cc;
    request.Method = "post";
    
    // Add UserAgent
    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
    

提交回复
热议问题