The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to use XML API with Asp.net

Discussion in 'cPanel Developers' started by rawat, May 21, 2010.

  1. rawat

    rawat Member

    Joined:
    May 21, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    How to use XML API with Asp.net ,please help for coding,how to use XML API for Create Account .

    Actually i m using

    XML and JSON APIs

    But facing Authentication Error.and I don't know PHP or Pearl.....

    ThanX
    rawat
     
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi Rawat,

    I'm not too familiar with ASP, but I imagine it shouldn't be hard. Your ASP script must:
    1) make a URL request
    2) that contains proper authentication headers

    You can test how the XML-API works directly in your browser. Manually logging into your server (this will set the authentication data in subsequent headers) and then make a direct XML-API call. A good test URL would be:
    Code:
    http://yourserver.com:2086/xml-api/version
    
    Maybe someone with ASP experience can provide an example piece of code.

    Regards,
    -DavidN


    UPDATE: placed example url in code block so the wysiwyg doesn't try to make a direct URL
     
    #2 cPanelDavidN, May 22, 2010
    Last edited: May 24, 2010
  3. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    if I remember correctly the work flow for .NET auth header via the default method is:

    1. Client sets up request object
    2. client sends request to server w/o auth header
    3. server responds stating that auth is needed
    4. client sends request again w/ auth header
    5. server accepts authentication

    The problem is that the way that cpsrvd responds to auth requests doesn't work with how .NET handles HTTP Basic auth in it's default implementation (this can be said for apache under certain configurations as well).

    The change that has to happen, is that:

    1. Client set up request object
    2. client sends request w/ auth headers
    3. server authenticates

    check out:

    ie-soft.de Blog - C#: Create a WebRequest with HTTP Basic Authentication

    for an example of how to do this, if this does not work for you, I've seen a few examples on how to do this and will hunt one down for you.
     
  4. rawat

    rawat Member

    Joined:
    May 21, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Authentication Error

    Hello Sir,

    when i m trying to create Account with XML API after login in cPanel Control Panel

    http://174.36.185.116:2086/xml-api/...p=n&cpmod=x3&password=h@rd2gu3ss!p@ss&contact email=username@domain2011.info&domain=domain2011.info&useregns=0&reseller=0

    Account Created but i m by Asp.net application,Execute Authentication Error............

    Pls reply and give me a example for CreateAccount with XML API with Asp.net

    ThanX
     
  5. rawat

    rawat Member

    Joined:
    May 21, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    XML API Authentication

    Hello Sir,

    I m trying this code for Authentication but facing Error

    string url = "http://test.com:2086";
    WebRequest myReq = WebRequest.Create(url);

    string username = "Test";
    string password = "test";
    string usernamePassword = username + ":" + password;
    CredentialCache mycache = new CredentialCache();
    mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
    myReq.Credentials = mycache;
    myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

    WebResponse wr = myReq.GetResponse();
    Stream receiveStream = wr.GetResponseStream();
    StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
    string content = reader.ReadToEnd();
     
  6. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    So, I just verified that this works for authentication:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    
    namespace ListAccount
    {
        class Program
        {
            static void Main(string[] args)
            {
                string url = "http://129.169.1.2:2086/xml-api/listaccts";
                HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
    
                string user = "root";
                string pwd = "x";
    
                string auth = "Basic " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(user + ":" + pwd));
                req.PreAuthenticate = true;
                req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
                req.Headers.Add("Authorization", auth);
    
                req.UserAgent = "cPanel .NET C# authenticator test script";
                WebResponse resp = req.GetResponse();
                Stream receiveStream = resp.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();
    
                System.Console.WriteLine(content);
                System.Console.Read();   
            }
        }
    }
    
     
  7. rawat

    rawat Member

    Joined:
    May 21, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    XML API For Create Account with ASP.net

    Hello Sir,

    but we will call XML API for Create Account with XML API.Please Provide a example....

    ThanX


    -------------------------







     
  8. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
  9. B12Org

    B12Org Well-Known Member

    Joined:
    Jul 15, 2003
    Messages:
    692
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    cPanel Access Level:
    Root Administrator
    When I try that it works well for everything except for create account - that one always throws an exception - although the command does work as I get the account created email and if I try again it immediately fails becuase the user exists.

    Per the message the error occurs here - on the 2nd line





    it seems to occur on any of those commands that send out emails - create account, suspend account, etc - but none of the other commands do that.

    We dont use proxies or anything like that.
    Any ideas why?
     
    #9 B12Org, Aug 20, 2010
    Last edited: Aug 20, 2010
  10. Eric

    Eric Administrator
    Staff Member

    Joined:
    Nov 25, 2007
    Messages:
    746
    Likes Received:
    11
    Trophy Points:
    18
    Location:
    Texas
    cPanel Access Level:
    Root Administrator
    Howdy,

    I'm able to use the same program Matt posted and changed the string url line to be this
    Code:
    string url = "http://grimlock:2086/xml-api/createacct?username=myuser&password=test$1234&domain=domain.tld";
    I get a created account. I'm using a simple console app. Give that a try. If you have problems let me know I'll help as best I can.

    Thanks!
     
  11. B12Org

    B12Org Well-Known Member

    Joined:
    Jul 15, 2003
    Messages:
    692
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    cPanel Access Level:
    Root Administrator
    Hi,

    We are using this:

    which basically is sent using this code
    PHP:
        private String SendRequest(String methodString variablesString server)
        {
            
    //Dont bail from a self signed cert or other cert error
            
    if (Global.UseSSL)
                
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidation);

            
    String url String.Format("http{0}://{1}:{2}/xml-api/{3}?{4}", Global.UseSSL "s" ""server, Global.UseSSL "2087" "2086"methodvariables);
            
    HttpWebRequest req HttpWebRequest.Create(url) as HttpWebRequest;

            
    String auth String.Format("Basic {0}"Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(String.Format("{0}:{1}", Global.WHMUser, Global.WHMPass))));
            
    req.PreAuthenticate true;
            
    req.AuthenticationLevel System.Net.Security.AuthenticationLevel.MutualAuthRequested;
            
    req.Headers.Add("Authorization"auth);

            
    WebResponse resp req.GetResponse();
            
    Stream receiveStream resp.GetResponseStream();
            
    StreamReader reader = new StreamReader(receiveStreamEncoding.UTF8);
    [
    B]        String strResponse reader.ReadToEnd();[/B]

            return 
    strResponse;
        }

    The bold line throws an exception every single time only when using the createaccount or un/suspendaccount functions. None of the other 20 method names that we have tried cause the error (like generate csr, install IPs, etc).

    seems wierd that its always these two that cause the error - and always the same error on the same line.

    Can you provide the code you are using, or perhaps a tweak to what we are doing above?
     
  12. Eric

    Eric Administrator
    Staff Member

    Joined:
    Nov 25, 2007
    Messages:
    746
    Likes Received:
    11
    Trophy Points:
    18
    Location:
    Texas
    cPanel Access Level:
    Root Administrator
    Howdy,

    I removed the trailing & from your request and I was able to make it work in my test program.

    Thanks!
     
  13. B12Org

    B12Org Well-Known Member

    Joined:
    Jul 15, 2003
    Messages:
    692
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    cPanel Access Level:
    Root Administrator
    I removed the trailing ampersand and the problem still persists.

     
  14. Eric

    Eric Administrator
    Staff Member

    Joined:
    Nov 25, 2007
    Messages:
    746
    Likes Received:
    11
    Trophy Points:
    18
    Location:
    Texas
    cPanel Access Level:
    Root Administrator
    Howdy,

    For the sake of testing would you try using the non https ports? I'd like to rule out your https/self-signed certificate code. If you'd like to encrypt the data you can still use an ssh tunnel via putty.

    Thanks!
     
  15. B12Org

    B12Org Well-Known Member

    Joined:
    Jul 15, 2003
    Messages:
    692
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    cPanel Access Level:
    Root Administrator
    Eric,

    Actually we are not using SSL atm, so these errors are not specific to ports unfortunately.
     
  16. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    When you make this connection, does anything come up in /usr/local/cpanel/logs/access_log or /usr/local/cpanel/logs/error_log
     
  17. B12Org

    B12Org Well-Known Member

    Joined:
    Jul 15, 2003
    Messages:
    692
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Seattle Washington
    cPanel Access Level:
    Root Administrator
    access log yes
    error log no
     
Loading...

Share This Page