cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
8608
Views
5
Helpful
2
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

2 Replies 2

sathech
Level 1
Level 1

Hello Nitin,

          This is Satheesha Gowda from ISE - Guest Dev team and i will be happy to help you on this.

As I can clearly see in my bare eyes is that request payload  is wrong( look out for red color double quote) and missing few of the important fields like sponsor user name and portal ID. and also i have provided the right payload, let me know if you still have question for me.

Thanks!

SG


<?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>



Right Request Payloads goes as follows.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ns4:guestuser description="A Test Guest"  name="TestGuestDuration" xmlns:ers="ers.ise.cisco.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns4="identity.ers.ise.cisco.com">

<customFields/>

<guestAccessInfo> 

<fromDate>10/09/2016 00:00</fromDate> 

<groupTag></groupTag>   

<location>San Jose</location>   

<toDate>10/19/2016 00:00</toDate>  

<validDays>10</validDays>

</guestAccessInfo>

<guestInfo>   

<emailAddress>Test.Guest@ISE.com</emailAddress>  

   <enabled>true</enabled>   

   <notificationLanguage>English</notificationLanguage>   

   <password>p@55w@rd</password>   

   <phoneNumber>01895 267231</phoneNumber>   

   <userName>User0001</userName>

</guestInfo>

<guestType>Contractor (default)</guestType>

<personBeingVisited>sponsor-guest@ISE.com</personBeingVisited>

<portalId>ba835850-6e5e-11e6-8f6a-005056873bd0</portalId>

<reasonForVisit>To have a look round</reasonForVisit>

<sponsorUserId>sponsor</sponsorUserId>

<sponsorUserName>sponsor</sponsorUserName>

<status/><statusReason/>

</ns4:guestuser>

Hi Satheesha,

You are correct. My XML was not in proper format and after making necessary changes in the xml, now I am able to create a guest user account using Cisco ISE 2.1 REST API from my application.

Thanks for your support.

Nitin Gupta

Sr. Software Engineer - Software Services

Velocis Systems Pvt. Ltd.

M: +91 9910622263 | Tel: +91 120 473 7500 (Extn 378) | F: +91 120 473 7546

A-25, Sector - 67, Noida - 201301, Uttar Pradesh, India | www.velocis.in