Visual Studio debugging WebApi

MB34

I have written a WebApi project in VS2013. I have also written an MVC4 application to test it in VS2013 on the same machine.

I run the WebApi project in VS2013, it uses localhost:49494 as server:port I then run the test project in VS2013, it uses localhost:49319 as server:port.

I calling a route in my WebApi from my test project, I get a response of 401 (Unauthorized). I AM NOT using the Authorize attribute on my WebApi functions. I do not send WWW-Authenticate header from my test project either.

Why would I get this? I just don't understand it. When I run the URL for the WebApi call in the browser, I get the desired result.

This is the HTML calling the MVC4 action:

<!DOCTYPE html>
<html>
    <head>
        <title>Webinar Registration Test </title>
    </head>
    <body>
        <div class="document">
        <form name="LoginForm" action="/Home/WBLogin" method="post">
            <input type="submit" value="Login" />
        </form>
        </div>
    </body>
</html>

This is the MVC4 Action method that calls the WebApi:

public ActionResult WBLogin()
{
    string uri = "http://localhost:49494/api/Webinar/WBLogin";
    AuthModel auth = new AuthModel();
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
    request.Accept = "application/json";
    request.Method = "GET";
    try
    {
        var response = request.GetResponse();

        //the following lines duplicate the response stream so we can read it for
        //deserialization and also re-read it and write it out.

        using (MemoryStream ms = new MemoryStream())
        {
            var stream = response.GetResponseStream();
            stream.CopyTo(ms);
            ms.Position = 0;
            stream.Close();

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ResponseDirectLogin));
            var deserialized = (ResponseDirectLogin)ser.ReadObject(ms);
            auth.OauthToken = deserialized.AccessToken;
            auth.OrganizerKey = deserialized.OrganizerKey;
        }
    }
    catch (WebException e)
    {
        if (e.Response != null) {
            using (var sr = new StreamReader(e.Response.GetResponseStream()))
                ViewBag.Error = sr.ReadToEnd();
        }
        else
        {
            ViewBag.Error = String.Concat("Message: ", e.Message, " Status: ", e.Status);
        }
    }
    Registrant User = new Registrant();
    User.OauthToken = auth.OauthToken;
    User.OrganizerKey = auth.OrganizerKey;                              
    User.WebinarKey = "9999999999999999999";
    return RedirectToAction("WBRegister", "Home", User);
}

This is the WebApi method:

public class WebinarController : ApiController
{

    [HttpGet, Route("api/Webinar/WBLogin")]
    public IHttpActionResult WBLogin()
    {
        // The Login Model contains the Login credentials for our GTW account
        LoginModel lg = new LoginModel();

        // first we need to create the uri for the web request
        string uri = String.Format("https://api.citrixonline.com/oauth/access_token?grant_type=password&user_id={0}&password={1}&client_id={2}",
                         lg.UserId, lg.Password, lg.APIKey);

        // then the request to login is created and sent. From the response
        // we need to store at least the access token and the organizer key
        // to use for further calls

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request.Accept = "application/json";
        request.ContentType = "application/json";

        try
        {
            var response = request.GetResponse();

            //the following lines duplicate the response stream so we can read it for
            //deserialization and also re-read it and write it out.

            using (MemoryStream ms = new MemoryStream())
            {
                var stream = response.GetResponseStream();
                stream.CopyTo(ms);
                ms.Position = 0;
                stream.Close();

                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ResponseDirectLogin));
                var deserialized = (ResponseDirectLogin)ser.ReadObject(ms);
                LoginResponse lr = new LoginResponse();
                lr.OauthToken = deserialized.AccessToken;
                lr.OrganizerKey = deserialized.OrganizerKey;
                string json_result = JsonConvert.SerializeObject(lr);
                return Ok(json_result);
            }
        }
        catch (WebException e)
        {
            using (var sr = new StreamReader(e.Response.GetResponseStream()))
            {
                LoginErrorResponse ler = new LoginErrorResponse();
                ler.Message = sr.ReadToEnd();
                string json_result = JsonConvert.SerializeObject(ler);
                return BadRequest(json_result);
            }
        }
    }

    // other methods here...

}
MB34

It was a certificate issue. My sysadmin had to install their certificate on our server to allow this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Visual Studio debugging WebApi

From Dev

Debugging WebApi in Visual Studio returns no response to MVC app

From Dev

Dynamic debugging with visual studio

From Dev

Dynamic debugging with visual studio

From Dev

Debugging Visual Studio

From Dev

Visual Studio 2015 Debugging vs Not Debugging Views

From Dev

Debugging Azure functions in Visual Studio

From Dev

Visual Studio Debugging with Source Maps

From Dev

Visual Studio Code debugging with AngularJS

From Dev

Debugging in Visual Studio 2013 express

From Dev

Visual Studio mixed projects debugging

From Dev

Application Debugging is disabled in Visual Studio 2012 on Classic ASP Debugging

From Dev

Debugging with visual studio multi-threaded app

From Dev

Visual Studio 2012 Launches Wrong Project For Debugging

From Java

Visual Studio debugging/loading very slow

From Java

Debugging with command-line parameters in Visual Studio

From Dev

Ignore a Project in Visual Studio while debugging

From Dev

Start Debugging from Visual Studio to Second Monitor

From Dev

Visual Studio Code - Debugging a spawned process

From Dev

Linux .NET remote debugging from Visual Studio

From Dev

Visual Studio clears output file at end of debugging

From Dev

Visual Studio 2015 Debugging not stopping at breakpoints

From Dev

Visual Studio "Start xslt debugging" option not visible

From Dev

'No disk in the Drive' error when debugging in Visual Studio

From Dev

Debugging Visual Studio web application in Release Mode

From Dev

Javascript Errors when Debugging in Visual Studio 2013

From Dev

Visual Studio 2015 locks DLL during debugging

From Dev

MEX File - Debugging LIBSVM in Visual Studio

From Dev

Debugging the InitializeComponent function in the Visual Studio designer

Related Related

  1. 1

    Visual Studio debugging WebApi

  2. 2

    Debugging WebApi in Visual Studio returns no response to MVC app

  3. 3

    Dynamic debugging with visual studio

  4. 4

    Dynamic debugging with visual studio

  5. 5

    Debugging Visual Studio

  6. 6

    Visual Studio 2015 Debugging vs Not Debugging Views

  7. 7

    Debugging Azure functions in Visual Studio

  8. 8

    Visual Studio Debugging with Source Maps

  9. 9

    Visual Studio Code debugging with AngularJS

  10. 10

    Debugging in Visual Studio 2013 express

  11. 11

    Visual Studio mixed projects debugging

  12. 12

    Application Debugging is disabled in Visual Studio 2012 on Classic ASP Debugging

  13. 13

    Debugging with visual studio multi-threaded app

  14. 14

    Visual Studio 2012 Launches Wrong Project For Debugging

  15. 15

    Visual Studio debugging/loading very slow

  16. 16

    Debugging with command-line parameters in Visual Studio

  17. 17

    Ignore a Project in Visual Studio while debugging

  18. 18

    Start Debugging from Visual Studio to Second Monitor

  19. 19

    Visual Studio Code - Debugging a spawned process

  20. 20

    Linux .NET remote debugging from Visual Studio

  21. 21

    Visual Studio clears output file at end of debugging

  22. 22

    Visual Studio 2015 Debugging not stopping at breakpoints

  23. 23

    Visual Studio "Start xslt debugging" option not visible

  24. 24

    'No disk in the Drive' error when debugging in Visual Studio

  25. 25

    Debugging Visual Studio web application in Release Mode

  26. 26

    Javascript Errors when Debugging in Visual Studio 2013

  27. 27

    Visual Studio 2015 locks DLL during debugging

  28. 28

    MEX File - Debugging LIBSVM in Visual Studio

  29. 29

    Debugging the InitializeComponent function in the Visual Studio designer

HotTag

Archive