cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1914
Views
0
Helpful
3
Replies

Getting "The remote server returned an error: (400) Bad Request." error when trying to connect to Cisco ISE 2.1 using ASP.NET MVC Application to consume Guest REST API

nitin.gupta
Level 1
Level 1

Hi All,

I am new to the Cisco ISE 2.1 and trying to build an ASP.NET MVC 5 application that will create Guest Users using the Guest REST API.

While I am trying to get user details (created using sponsor portal by sponsor user "vspl"), I am able to get the information.

As I am trying to create a guest user account using the Guest REST API from my application and trying get response (HttpWebRequest.GetResponse()), an error "The remote server returned an error: (400) Bad Request." is raising. below is my code:

==============================================================================================

public string postXMLData()
        {
            destinationUrl = @"https://ise.demo.com:9060/ers/config/guestuser/";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(destinationUrl);
            request.Method = "POST";                                                                                      // to create a new guest user account
            request.ContentType = "application/vnd.com.cisco.ise.identity.guestuser.2.0+xml";
            request.Accept = "application/vnd.com.cisco.ise.identity.guestuser.2.0+xml";
            request.AutomaticDecompression = DecompressionMethods.GZip;

            string credidentials = "vspl:Password"; //// "vspl is the Sponsor user"
            string authCode = Convert.ToBase64String(Encoding.Default.GetBytes(credidentials)); //to convert user id and password as per RFC 2617
            request.Headers.Add("Authorization", "Basic " + authCode);
            

            //// XML data to post to the REST API's POST method
            requestXml = @"<?xml version=""1.0"" encoding= ""utf-8"" standalone= ""yes""?><ns2:guestuser xmlns:ns2=""identity.ers.ise.cisco.com""><guestAccessInfo><fromDate>08/17/2016 19:01</fromDate><location>San Jose</location><toDate>11/14/2016 19:01</toDate><validDays>90</validDays></guestAccessInfo><guestInfo><company>VSPL</company><emailAddress>nitin.gupta1@velocis.co.in</emailAddress><enabled>false</enabled><firstName>Nitin</firstName><lastName>Gupta</lastName><notificationLanguage>English</notificationLanguage><phoneNumber>9988880000</phoneNumber><smsServiceProvider>Global Default</smsServiceProvider></guestInfo><guestType>Contractor (default)</guestType><personBeingVisited>deepesh.rastogi@gmail.com</personBeingVisited><reasonForVisit>Meeting</reasonForVisit></ns2:guestuser>";

            byte[] bytes;
            bytes = System.Text.Encoding.UTF8.GetBytes(requestXml);
            request.ContentLength = bytes.Length;
           
            try
            {
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();

                //// Below line is throwing "The remote server returned an error: (400) Bad Request." error
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream responseStream = response.GetResponseStream();
                        string responseStr = new StreamReader(responseStream).ReadToEnd();
                        return responseStr;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }

===========================================================================

Can somebody help me on this.

Thanks in advance.

Nitin Gupta

3 Replies 3

jan.nielsen
Level 7
Level 7

Just quickly looking over your request, you are missing the portalid definition in your xml. Also i believe that the customFields difinition is now mandatory in ISE 2.1. ns:2 should according to the ISE documentation be ns:3.

Example from docu :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:guestuser name="guestUser" id="123456789" description="ERS Example user " xmlns:ns2="ers.ise.cisco.com" xmlns:ns3="identity.ers.ise.cisco.com">
<customFields>
<entry>
<key>some key</key>
<value>some value</value>
</entry>
<entry>
<key>another key</key>
<value>and its value</value>
</entry>
</customFields>
<guestInfo>
<emailAddress>email@some.uri.com</emailAddress>
<enabled>true</enabled>
<phoneNumber>3211239034</phoneNumber>
<smsServiceProvider>GLobal Default</smsServiceProvider>
<userName>DS3ewdsa34wWE</userName>
</guestInfo>
<guestType>Contractor</guestType>
<portalId>23423432523</portalId>
<sponsorUserName>Mr Spons</sponsorUserName>
</ns3:guestuser>

Hi nielsen,

Thanks for the reply but as per page no. 142 in api ref documentation above Table No. 7-33, only guestType is required field.

I have also tried to add Portal Id, but issue still remains.

Thanks

That section does not indicate that guestType is the only required field, it just states that there has to be a guestType in the request.

Did you put an actual portalid number in the request from your ise configuration?

What did you put in the customfields section? If you don't have anything to put there, you can just use 

<customFields></customFields>