简体   繁体   English

Visual Studio调试WebApi

[英]Visual Studio debugging WebApi

I have written a WebApi project in VS2013. 我在VS2013中编写了一个WebApi项目。 I have also written an MVC4 application to test it in VS2013 on the same machine. 我还编写了一个MVC4应用程序以在同一台计算机上的VS2013中对其进行测试。

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. 我在VS2013中运行WebApi项目,它将localhost:49494用作server:port,然后在VS2013中运行测试项目,将localhost:49319用作server:port。

I calling a route in my WebApi from my test project, I get a response of 401 (Unauthorized). 我从测试项目中调用WebApi中的路由,得到的响应为401(未经授权)。 I AM NOT using the Authorize attribute on my WebApi functions. 我未在WebApi函数上使用Authorize属性。 I do not send WWW-Authenticate header from my test project either. 我也不从测试项目中发送WWW-Authenticate标头。

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. 当在浏览器中运行WebApi调用的URL时,得到了所需的结果。

This is the HTML calling the MVC4 action: 这是调用MVC4动作的HTML:

<!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: 这是调用WebApi的MVC4 Action方法:

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: 这是WebApi方法:

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

}

I have a strong feeling it is a global filter somewhere. 我强烈认为它是某个地方的全局过滤器。 Check the App_Start\\FilterConfig.cs and make sure you are not appending an AuthorizeAttribute . 检查App_Start\\FilterConfig.cs并确保您没有附加AuthorizeAttribute

It was a certificate issue. 这是一个证书问题。 My sysadmin had to install their certificate on our server to allow this. 我的系统管理员必须在我们的服务器上安装其证书才能允许这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM