简体   繁体   English

调用aspx页的EXE程序获取:401-未经授权:由于凭据无效,访问被拒绝

[英]EXE program calling aspx page gets: 401 - Unauthorized: Access is denied due to invalid credentials

I'm creating an executable program that uses HttpClient to call a URL. 我正在创建一个使用HttpClient调用URL的可执行程序。 I try using the DefaultNetworkCredentials as I do not know the user id and password of the server. 我尝试使用DefaultNetworkCredentials因为我不知道服务器的用户ID和密码。 The following code runs successfully on my local machine, but it will get error when deployed to the server. 以下代码在我的本地计算机上成功运行,但是在部署到服务器时会出错。

在此处输入图片说明

My code: 我的代码:

HttpClientHandler authtHandler = new HttpClientHandler()
{
    Credentials = CredentialCache.DefaultNetworkCredentials
};

// Call the URL                    
using (HttpClient httpClient = new HttpClient(authtHandler))
{
    var formContent = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("testingkey", "testingvalue")
    });

    // Make it sync instead of async by using .Result
    var result = httpClient.PostAsync(url, formContent).Result;

    // Get the content, for verification 
    var content = result.Content;
}

DefaultNetworkCredentials is the authentication credentials for the current security context in which the application is running. DefaultNetworkCredentials是运行应用程序的当前安全上下文的身份验证凭据。 For client side application, these are usually the Windows credentials of the user who is running the application. 对于客户端应用程序,这些通常是运行该应用程序的用户的Windows凭据。

If the user does not have rights over the URL over what you are using the HttpClient then error will come. 如果用户没有使用HttpClient的URL的权限,则将出现错误。 To make it work you 为了让你工作

  1. either have to impersonate the user who has access over that application or 要么假冒可以访问该应用程序的用户,要么
  2. run your EXE using system defined accounts like Network Service credentials and ensure that HTTP application can be accessed using the same. 使用系统定义的帐户(如网络服务凭据)运行EXE,并确保可以使用HTTP来访问HTTP应用程序。

暂无
暂无

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

相关问题 C# 的图表 API:401 - 未经授权:由于凭据无效,访问被拒绝。 在 /planner/plans - Graph API for C#: 401 - Unauthorized: Access is denied due to invalid credentials. on /planner/plans 未经授权:访问被拒绝,因为凭证无效。 用于共享点的 Microsoft Graph API - Unauthorized: Access is denied due to invalid credentials . Microsoft Graph API for sharepoint IIS8未经授权:由于凭据无效而拒绝访问 - IIS8 Unauthorized: Access is denied due to invalid credentials 由于无效的 OAuth 签名而涉及查询参数时,GET 失败并显示 401(未经授权) - GET fails with 401 (Unauthorized) when query parameter is involved due to invalid OAuth signature StatusCode:401,ReasonPhrase:使用C#通过HTTPClient调用Post方法时,显示“未授权” - StatusCode: 401, ReasonPhrase: 'Unauthorized' gets displayed when calling a Post Method through HTTPClient using C# SC.EXE-从C#调用-访问被拒绝 - SC.EXE - Calling from C# - Access Denied 错误 401 Unauthorized 即使通过身份验证也无法访问我的主页 - Error 401 Unauthorized can't access to my home page even with the authentication WebClient 在传递凭据时出现 401 未经授权的错误 - WebClient Giving 401 unauthorized error when credentials are passed 尝试访问REST API时为401(未经授权) - 401 (Unauthorized) when trying to access REST API 401 Unauthorized在GET请求(https)上使用正确的凭据返回 - 401 Unauthorized returned on GET request (https) with correct credentials
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM