程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> Web Service Error: “The request failed with HTTP status 401: Unauthorized.”

Web Service Error: “The request failed with HTTP status 401: Unauthorized.”

編輯:.NET實例教程

解決方法:

Samples.TimeWebService ws = new Samples.TimeWebService();

//下面這句提供授權
ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
string currentTime = ws.GetCurrentTime();

原理以及詳細分析原文如下:兩種解決方法第一種我嘗試無效,並且從安全性上來說也是代碼授權比較合理

原文Copy 

I’ve come across a number of developers who have run into this issue when trying to call a web service in Visual Studio 2005 Beta 2. The symptom of the problem is that when trying to call a web service that is running on the "ASP.Net Development Server" from another application, the web service throws a WebException with the message: “The request failed with HTTP status 401: Unauthorized.”

First and foremost, this issue is not related to debugging. If you run the application outside of Visual Studio, you’ll see the same result. Also noteworthy to mention is that when you run the web service by itself, everything works fine. It’s only when you call the web service from another application that it fails.

Why do I get this error?

The reason for this error is that by default, a web service project in Visual Studio 2005 Beta 2 required NTLM authentication. When you call the web service from another application, the application trIEs to call into the web service with anonymous Access and fails. But, when you call the web service from the web service test page in Internet Explorer, your credentials are passed from IE to the web service which means that the call succeeds.

How do I fix this error?

There are two ways to fix this error.

1. Disable NTLM Authentication in Project PropertIEs
If your web service is not intended to require authentication, the easIEst way to fix this error is to turn off NTML authentication in a web service project. To do this, right click on the web service project and select “Property Pages”. On the “Start Options” page,
uncheck “NTLM Authentication”. NOTE: disabling this option has security implications if you are running the web service in a Terminal Server environment. If the web service is on your local Machine and it’s not being used in a Terminal Server environment, then you should be fine.

2. Pass User''s Credentials from Calling Application to Web Service
If your web service is intended to require authentication and not anonymous Access, then pass the credentials of the user from the application to the web service. Here is an example:

Samples.TimeWebService ws = new Samples.TimeWebService();
ws.Credentials = System.Net.CredentialCache.DefaultCredentials;
string currentTime = ws.GetCurrentTime();

HabibH.
6:46 PM 5/25/2005.

原文地質http://blogs.msdn.com/habibh/archive/2005/05/25/421954.ASPx

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved