在内部网络上运行代码时出现不需要的代理身份验证问题。代码问题?

发布于 2024-11-05 07:30:03 字数 2856 浏览 1 评论 0原文

我编写了一个测试应用程序来根据内部 RESTful 服务验证一些用户详细信息。

如果我直接通过浏览器发出请求,我会收到来自服务的响应,但如果我通过代码发出请求,响应会指出我需要代理服务器身份验证。

虽然我可以提供用户可配置的设置,以便可以传递代理参数,但感觉不对,而且我不确定我的代码是否不正确。

下面是失败的代码片段,后面是包含代理详细信息的代码片段。

/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
    bool valid = false;

    try
    {
        // Create the XML to be passed as the request
        XElement root = BuildRequestXML("LOGON");

        // Add the action to the service address
        Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");

        // Make the RESTful request to the service using a POST
        using (HttpResponseMessage response = new HttpClient().Post(serviceReq, HttpContentExtensions.CreateDataContract(itrent)))
        {
            // Retrieve the response for processing
            response.Content.LoadIntoBuffer();
            string returned = response.Content.ReadAsString();

            // TODO: parse the response string for the required data

        }
    }
    catch (Exception ex)
    {
        Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
        valid = false;
    }

    return valid;
}

现在对于可以工作但有问题的块......

/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
    bool valid = false;

    try
    {
        // Create the XML to be passed as the request
        XElement root = BuildRequestXML("LOGON");

        // Add the action to the service address
        Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");

        // Create the client for the request
        using (HttpClient client = new HttpClient())
        {

            // Create a proxy to get around the network issues and assign it to the http client
            WebProxy px = new WebProxy( <Proxy server address>, <Proxy Server Port> );
            px.Credentials = new NetworkCredential( <User Name>, <Password>, <Domain> );
            client.TransportSettings.Proxy = px;

            // Mare the RESTful request
            HttpResponseMessage response = client.Post(serviceReq, HttpContentExtensions.CreateDataContract(root));

            // Retrieve the response for processing
            response.Content.LoadIntoBuffer();
            string returned = response.Content.ReadAsString();

            // TODO: parse the response string for the required data

        }
    }
    catch (Exception ex)
    {
        Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
        valid = false;
    }

    return valid;
}

非常感谢所有建议。 干杯。

I have written a test application to validate some user details against an internal RESTful service.

If I make the request directly through a browser I get a response from the service BUT if I make the request via my code, the response states that I need proxy server authentication.

Whilst I could provide user configurable settings so that the proxy parameters could be passed it just feels wrong and I'm not sure if my code is incorrect.

Below is the code snippet that fails, followed by the snippet with the proxy details.

/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
    bool valid = false;

    try
    {
        // Create the XML to be passed as the request
        XElement root = BuildRequestXML("LOGON");

        // Add the action to the service address
        Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");

        // Make the RESTful request to the service using a POST
        using (HttpResponseMessage response = new HttpClient().Post(serviceReq, HttpContentExtensions.CreateDataContract(itrent)))
        {
            // Retrieve the response for processing
            response.Content.LoadIntoBuffer();
            string returned = response.Content.ReadAsString();

            // TODO: parse the response string for the required data

        }
    }
    catch (Exception ex)
    {
        Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
        valid = false;
    }

    return valid;
}

Now for the chunk that works but has issues...

/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
    bool valid = false;

    try
    {
        // Create the XML to be passed as the request
        XElement root = BuildRequestXML("LOGON");

        // Add the action to the service address
        Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");

        // Create the client for the request
        using (HttpClient client = new HttpClient())
        {

            // Create a proxy to get around the network issues and assign it to the http client
            WebProxy px = new WebProxy( <Proxy server address>, <Proxy Server Port> );
            px.Credentials = new NetworkCredential( <User Name>, <Password>, <Domain> );
            client.TransportSettings.Proxy = px;

            // Mare the RESTful request
            HttpResponseMessage response = client.Post(serviceReq, HttpContentExtensions.CreateDataContract(root));

            // Retrieve the response for processing
            response.Content.LoadIntoBuffer();
            string returned = response.Content.ReadAsString();

            // TODO: parse the response string for the required data

        }
    }
    catch (Exception ex)
    {
        Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
        valid = false;
    }

    return valid;
}

All suggestions are gratefully received.
Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-11-12 07:30:03

当您浏览到该服务时,您正在使用登录用户的代理设置。当您尝试通过 Web 服务通过代理时,它可能以未配置代理设置的 ASP.NET 帐户运行。

您可以按照

  • 您的建议提供可配置的代理设置在
  • 配置了正确代理设置的帐户下运行 Web 服务(也许使用 Windows 身份验证?)
  • 尝试将部分添加到您的 web.config
    ;<代理usesystemdefault =“true”/>

此处对此进行了很好的讨论

When you browse to the service you are using the proxy settings for the signed on user. When you try to go through a proxy through the web service, it is probably running as the ASP.NET account which does not have proxy settings configured.

You can either

  • Provide configurable proxy settings as you suggest
  • Run the web service under an account that is configured with the correct proxy settings (using Windows Authentication perhaps?)
  • Try adding section to your web.config
    <system.net> <defaultProxy useDefaultCredentials="true"> <proxy usesystemdefault="true"/> </defaultProxy> </system.net>

Good discussion on it here

凉城 2024-11-12 07:30:03

将此代码放入您的 app.config 中。它应该禁用代理

<system.net> 
  <defaultProxy 
    enabled="false" 
    useDefaultCredentials="false" > 
    <proxy/> 
    <bypasslist/> 
    <module/> 
  </defaultProxy> 
</system.net> 

Put this code to your app.config. It should disable proxy

<system.net> 
  <defaultProxy 
    enabled="false" 
    useDefaultCredentials="false" > 
    <proxy/> 
    <bypasslist/> 
    <module/> 
  </defaultProxy> 
</system.net> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文