473,594 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Authentication not working on HTTP-POST using NetworkCredenti al


I am programming what is to be a web service client that will use an
HTTP-POST to request and retrieve data. The remote server (written in java
for what it's worth) requires basic authentication as per RFC 2617
(http://www.faqs.org/rfcs/rfc2617.html). My attempts to authenticate are
failing. The server requires the header to be present with the request.
For security reasons, it will not reply in any way if the header is not
present.

More specifically, my attempts fail when attempting to attach a
'NetworkCredent ial' object to the 'Credentials' property of a
'HttpWebRequest ' object. If I create the header manually, everything works
fine. When attempting to do it 'the Microsoft Way' no authentication
information is sent in the header, even if I set 'PreAuthenticat e' = true.

What am I missing? Below are two examples. Each has the code to send the
request followed by the captured request header.
- Patrick

------------------------------------------------------------
<< the code that fails >>

(( assume reqBytes and SomeURI already set ))

request = (HttpWebRequest ) WebRequest.Crea te(SomeURI);

request.PreAuth enticate = true;
request.Credent ials = new NetworkCredenti al("JoeBlow","M ountainHo");

request.Timeout = 20 * 1000;
request.Method = "POST";
request.Content Type = "applicatio n/x-www-form-urlencoded";
request.Content Length = reqBytes.Length ;

Stream reqStream = request
reqStream.Write (reqBytes,0,req Bytes.Length);
reqStream.Close ();

------------------------------
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 1718
Expect: 100-continue
Connection: Keep-Alive
Host: me:10000

------------------------------------------------------------
<< the code that works>>

(( assume reqBytes and SomeURI already set ))

request = (HttpWebRequest ) WebRequest.Crea te(SomeURI);

// 'GetManualAutho rization' written by me to generate RFC2617-compliant
basic authentication header
request.Headers .Add("Authoriza tion", GetManualAuthor ization("JoeBlo w",
"MountainHo "));
request.Timeout = 20 * 1000;
request.Method = "POST";
request.Content Type = "applicatio n/x-www-form-urlencoded";
request.Content Length = reqBytes.Length ;

Stream reqStream = request
reqStream.Write (reqBytes,0,req Bytes.Length);
reqStream.Close ();

------------------------------
POST / HTTP/1.1
Authorization: BASIC Sm9lQmxvdzpNb3V udGFpbkhv
Content-Type: application/x-www-form-urlencoded
Content-Length: 1718
Expect: 100-continue
Connection: Keep-Alive
Host: me:10000
Nov 22 '05 #1
3 6891
Hi Patrick,

The reason you are not seeing the credentials passed on the
inital request to the web server is because Microsoft is following
section 2 of RFC 2617(http://www.faqs.org/rfcs/rfc2617.html)

Here’s the main benefit of using pre-authenticate. Suppose I’m going to
make 50
requests to <http://server/path/> and this URL is protected with Basic
authentication. On the first request, the client gets challenged by the
server and
sends back a second request which contains information that the server
accepts
(assuming auth succeeds) so it can send back the requested resource.
With the pre-authenticate property set to true:
The remaining 49 requests will include the authorization information in the
first
request they send to the server so the server will not challenge the client
and
force it to do another round trip before getting the resource.
The total number of roundtrips between client and server will be 51.
With the pre-authenticate property set to false:
The remaining 49 requests will not include the authorization information in
the
first request and will therefore be challenged by the server on each first
request
and will only get the desired resource after sending the authorization
header in
the second request.
The total number of roundtrips between client and server will be 100.
In other words, pre-authenticate=tr ue is one request shy of taking half the
time of
pre-authenticate=fa lse. Note that pre-authentication only works for Digest
and
Basic in v1.0. It can’t work for NTLM because it is connection-based
however the
fact that it is connection based means that you’ll only get challenged once
per
connection so it isn’t an issue if you are caching connections. In the
Whidbey
release of the .NET Framework we’ll also support pre-authentication for
Kerberos.

In order to get the inital request to send credentials, you will need to
use the
workaround of overriding the GetWebRequest method in the proxy code.

(Hack code obtained from the Internet)
The PreAuthenticate property on .NET's
System.Web.Serv ices.Protocols. SoapHttpClientP rotocol is supposed to force
the SOAP
client proxy to send credentials with the first request, rather than doing
the
challenge/response exchange. If you add the following code to your SOAP
Client
proxy, you can make PreAuthenticate work (this example is for basic
authentication) :
protected override System.Net.WebR equest
GetWebRequest(U ri uri) {
System.Net.Http WebRequest request =
(System.Net.Htt pWebRequest)bas e.GetWebRequest (uri);
if (this.PreAuthen ticate) {
System.Net.Netw orkCredential nc =
this.Credential s.GetCredential (uri,"Basic");
if (nc != null) {
byte[] credBuf =
new System.Text.UTF 8Encoding().
GetBytes(nc.Use rName + ":" + nc.Password);
request.Headers["Authorizat ion"] =
"Basic " + Convert.ToBase6 4String(credBuf );
}
}
return request;
}

This work around modifies the web service proxy class which is
automatically generated. This means every time someone updates a "web
reference" in
Dev Studio, they would need to reinsert the "hack" code.

Let me know if you have any questions or conerns.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Patrick Fogarty" <pa************ *@spam.hotmail. no.com>
Subject: Authentication not working on HTTP-POST using NetworkCredenti al
Date: Mon, 25 Aug 2003 13:49:49 -0400
Lines: 82
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <ev************ **@TK2MSFTNGP12 .phx.gbl>
Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet.webs ervices,microso ft.public.dotne t
framework.webse rvices,microsof t.public.dotnet .generalNNTP-Posting-Host: ool-182e5a0b.dyn.op tonline.net 24.46.90.11
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.webservices :1297
microsoft.publi c.dotnet.genera l:105997
microsoft.publi c.dotnet.framew ork.aspnet.webs ervices:19007X-Tomcat-NG: microsoft.publi c.dotnet.genera l
I am programming what is to be a web service client that will use an
HTTP-POST to request and retrieve data. The remote server (written in java
for what it's worth) requires basic authentication as per RFC 2617
(http://www.faqs.org/rfcs/rfc2617.html). My attempts to authenticate are
failing. The server requires the header to be present with the request.
For security reasons, it will not reply in any way if the header is not
present.

More specifically, my attempts fail when attempting to attach a
'NetworkCreden tial' object to the 'Credentials' property of a
'HttpWebReques t' object. If I create the header manually, everything works
fine. When attempting to do it 'the Microsoft Way' no authentication
information is sent in the header, even if I set 'PreAuthenticat e' = true.

What am I missing? Below are two examples. Each has the code to send the
request followed by the captured request header.
- Patrick

------------------------------------------------------------
<< the code that fails >>

(( assume reqBytes and SomeURI already set ))

request = (HttpWebRequest ) WebRequest.Crea te(SomeURI);

request.PreAut henticate = true;
request.Creden tials = new NetworkCredenti al("JoeBlow","M ountainHo");

request.Timeou t = 20 * 1000;
request.Meth od = "POST";
request.Conten tType = "applicatio n/x-www-form-urlencoded";
request.Conten tLength = reqBytes.Length ;

Stream reqStream = request
reqStream.Writ e(reqBytes,0,re qBytes.Length);
reqStream.Clos e();

------------------------------
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 1718
Expect: 100-continue
Connection: Keep-Alive
Host: me:10000

------------------------------------------------------------
<< the code that works>>

(( assume reqBytes and SomeURI already set ))

request = (HttpWebRequest ) WebRequest.Crea te(SomeURI);

// 'GetManualAutho rization' written by me to generate RFC2617-compliant
basic authentication header
request.Header s.Add("Authoriz ation", GetManualAuthor ization("JoeBlo w",
"MountainHo")) ;
request.Timeou t = 20 * 1000;
request.Meth od = "POST";
request.Conten tType = "applicatio n/x-www-form-urlencoded";
request.Conten tLength = reqBytes.Length ;

Stream reqStream = request
reqStream.Writ e(reqBytes,0,re qBytes.Length);
reqStream.Clos e();

------------------------------
POST / HTTP/1.1
Authorizatio n: BASIC Sm9lQmxvdzpNb3V udGFpbkhv
Content-Type: application/x-www-form-urlencoded
Content-Length: 1718
Expect: 100-continue
Connection: Keep-Alive
Host: me:10000


Nov 22 '05 #2
Peter -

I want to thank you for that thorough response.

I kind of suspected that was the case. Sometimes you have to read a RFC a
few hundred times to translate from theory to practical use. I had made
mention (to the authors of the server) that no challenge was being issued.
Unfortunately, especially in the industry that I am in, not responding (and
just closing the connection) in the absence of proper credentials is very
common. It prevents an accidental or deliberate probe of a URL from
divulging information that can be used to mount a subsequent attack.

The hack that you included below is similar to the one I did myself. I
merely put it in a method of a utility class rather than one of a derived
class.
- Patrick


"Peter Huang [MSFT]" <v-******@online.m icrosoft.com> wrote in message
news:XH******** ******@cpmsftng xa06.phx.gbl...
| Hi Patrick,
|
| The reason you are not seeing the credentials passed on the
| inital request to the web server is because Microsoft is following
| section 2 of RFC 2617(http://www.faqs.org/rfcs/rfc2617.html)
|
| Here’s the main benefit of using pre-authenticate. Suppose I’m going to
| make 50
| requests to <http://server/path/> and this URL is protected with Basic
| authentication. On the first request, the client gets challenged by the
| server and
| sends back a second request which contains information that the server
| accepts
| (assuming auth succeeds) so it can send back the requested resource.
| With the pre-authenticate property set to true:
| The remaining 49 requests will include the authorization information in
the
| first
| request they send to the server so the server will not challenge the
client
| and
| force it to do another round trip before getting the resource.
| The total number of roundtrips between client and server will be 51.
| With the pre-authenticate property set to false:
| The remaining 49 requests will not include the authorization information
in
| the
| first request and will therefore be challenged by the server on each first
| request
| and will only get the desired resource after sending the authorization
| header in
| the second request.
| The total number of roundtrips between client and server will be 100.
| In other words, pre-authenticate=tr ue is one request shy of taking half
the
| time of
| pre-authenticate=fa lse. Note that pre-authentication only works for Digest
| and
| Basic in v1.0. It can’t work for NTLM because it is connection-based
| however the
| fact that it is connection based means that you’ll only get challenged
once
| per
| connection so it isn’t an issue if you are caching connections. In the
| Whidbey
| release of the .NET Framework we’ll also support pre-authentication for
| Kerberos.
|
| In order to get the inital request to send credentials, you will need to
| use the
| workaround of overriding the GetWebRequest method in the proxy code.
|
| (Hack code obtained from the Internet)
| The PreAuthenticate property on .NET's
| System.Web.Serv ices.Protocols. SoapHttpClientP rotocol is supposed to force
| the SOAP
| client proxy to send credentials with the first request, rather than doing
| the
| challenge/response exchange. If you add the following code to your SOAP
| Client
| proxy, you can make PreAuthenticate work (this example is for basic
| authentication) :
| protected override System.Net.WebR equest
| GetWebRequest(U ri uri) {
| System.Net.Http WebRequest request =
| (System.Net.Htt pWebRequest)bas e.GetWebRequest (uri);
| if (this.PreAuthen ticate) {
| System.Net.Netw orkCredential nc =
| this.Credential s.GetCredential (uri,"Basic");
| if (nc != null) {
| byte[] credBuf =
| new System.Text.UTF 8Encoding().
| GetBytes(nc.Use rName + ":" + nc.Password);
| request.Headers["Authorizat ion"] =
| "Basic " + Convert.ToBase6 4String(credBuf );
| }
| }
| return request;
| }
|
| This work around modifies the web service proxy class which is
| automatically generated. This means every time someone updates a "web
| reference" in
| Dev Studio, they would need to reinsert the "hack" code.
|
| Let me know if you have any questions or conerns.
|
| Regards,
| Peter Huang
| Microsoft Online Partner Support
| Get Secure! www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights.
Nov 22 '05 #3
Hi Patrick,

I am glad that you have gotten a workaround yourself.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Patrick Fogarty" <pa************ *@spam.hotmail. no.com>
References: <ev************ **@TK2MSFTNGP12 .phx.gbl> <XH************ **@cpmsftngxa06 .phx.gbl>Subject: Re: Authentication not working on HTTP-POST using NetworkCredenti alDate: Tue, 26 Aug 2003 09:13:29 -0400
Lines: 118
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OK************ *@TK2MSFTNGP10. phx.gbl>
Newsgroups: microsoft.publi c.dotnet.genera l
NNTP-Posting-Host: ool-18ba9dd9.dyn.op tonline.net 24.186.157.217
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:106081
X-Tomcat-NG: microsoft.publi c.dotnet.genera l

Peter -

I want to thank you for that thorough response.

I kind of suspected that was the case. Sometimes you have to read a RFC a
few hundred times to translate from theory to practical use. I had made
mention (to the authors of the server) that no challenge was being issued.
Unfortunatel y, especially in the industry that I am in, not responding (and
just closing the connection) in the absence of proper credentials is very
common. It prevents an accidental or deliberate probe of a URL from
divulging information that can be used to mount a subsequent attack.

The hack that you included below is similar to the one I did myself. I
merely put it in a method of a utility class rather than one of a derived
class.
- Patrick


"Peter Huang [MSFT]" <v-******@online.m icrosoft.com> wrote in message
news:XH******* *******@cpmsftn gxa06.phx.gbl.. .
| Hi Patrick,
|
| The reason you are not seeing the credentials passed on the
| inital request to the web server is because Microsoft is following
| section 2 of RFC 2617(http://www.faqs.org/rfcs/rfc2617.html)
|
| Here’s the main benefit of using pre-authenticate. Suppose I’m going to
| make 50
| requests to <http://server/path/> and this URL is protected with Basic
| authentication. On the first request, the client gets challenged by the
| server and
| sends back a second request which contains information that the server
| accepts
| (assuming auth succeeds) so it can send back the requested resource.
| With the pre-authenticate property set to true:
| The remaining 49 requests will include the authorization information in
the
| first
| request they send to the server so the server will not challenge the
client
| and
| force it to do another round trip before getting the resource.
| The total number of roundtrips between client and server will be 51.
| With the pre-authenticate property set to false:
| The remaining 49 requests will not include the authorization information
in
| the
| first request and will therefore be challenged by the server on each first| request
| and will only get the desired resource after sending the authorization
| header in
| the second request.
| The total number of roundtrips between client and server will be 100.
| In other words, pre-authenticate=tr ue is one request shy of taking half
the
| time of
| pre-authenticate=fa lse. Note that pre-authentication only works for Digest| and
| Basic in v1.0. It can’t work for NTLM because it is connection-based
| however the
| fact that it is connection based means that you’ll only get challenged
once
| per
| connection so it isn’t an issue if you are caching connections. In the
| Whidbey
| release of the .NET Framework we’ll also support pre-authentication for
| Kerberos.
|
| In order to get the inital request to send credentials, you will need to
| use the
| workaround of overriding the GetWebRequest method in the proxy code.
|
| (Hack code obtained from the Internet)
| The PreAuthenticate property on .NET's
| System.Web.Serv ices.Protocols. SoapHttpClientP rotocol is supposed to force
| the SOAP
| client proxy to send credentials with the first request, rather than doing| the
| challenge/response exchange. If you add the following code to your SOAP
| Client
| proxy, you can make PreAuthenticate work (this example is for basic
| authentication) :
| protected override System.Net.WebR equest
| GetWebRequest(U ri uri) {
| System.Net.Http WebRequest request =
| (System.Net.Htt pWebRequest)bas e.GetWebRequest (uri);
| if (this.PreAuthen ticate) {
| System.Net.Netw orkCredential nc =
| this.Credential s.GetCredential (uri,"Basic");
| if (nc != null) {
| byte[] credBuf =
| new System.Text.UTF 8Encoding().
| GetBytes(nc.Use rName + ":" + nc.Password);
| request.Headers["Authorizat ion"] =
| "Basic " + Convert.ToBase6 4String(credBuf );
| }
| }
| return request;
| }
|
| This work around modifies the web service proxy class which is
| automatically generated. This means every time someone updates a "web
| reference" in
| Dev Studio, they would need to reinsert the "hack" code.
|
| Let me know if you have any questions or conerns.
|
| Regards,
| Peter Huang
| Microsoft Online Partner Support
| Get Secure! www.microsoft.com/security
| This posting is provided "as is" with no warranties and confers no rights.


Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
8068
by: John Davis | last post by:
Anyone knows how to create the username/password authorization dialog in ASP? Thanks, John
1
3067
by: Hugh McLaughlin | last post by:
Hello Everyone and thanks for your help in advance. I recently installed Visual Studio 2003 and .Net 1.1. I then converted one of my applications to 2003. However, I am running into a probelm with forms authenticaion. I have set up certain subfolders that are proected and should be redirected to a login page. However, when I try to access the protected pages, I receive an HTTP 403 error, you are not authorized to view this page,...
2
1272
by: Kian Goh | last post by:
Hi there, I am trying to use an entry level security for my resources website. I followed the procedures in the MS published Self-Paced Training Kit, everything seems working as expected. However, I found that the authentication cookie never expires. I thought the default timeout is 30 minutes. Please tell me if I miss any step...
1
1025
by: Galore | last post by:
Hello! I wonder if there's a way to a web site has both kinds of authentication: windows and forms. The web application I'm working on will be accessed by two kind of users: administrators, that will access it throught Intranet (windows authentication), and customers, that will access it thought Internet (forms authentication). Is there any way to check for the firts authentication method, and if it's not available, the second method...
2
3829
by: Dan | last post by:
hi ng, i have a problem with windows authentification. i want to forward every user who 1. is not authorized 2. or could not be authenticated to a login page -------------------
3
1316
by: Paul Mason | last post by:
Hi folks, An odd one for the start of this week. I have a web project that I have taken over from a colleague. He had the authentication set to windows, but I have now changed that to forms authentication by putting the following xml in we.config : <authentication mode="Forms">
1
1414
by: Shapper | last post by:
Hello, I am developing a web site where half of the pages are public and the other half are accessible only to registered users. The pages which are accessible only to registered users have content which can be visible or not according to the user access level. I was reading the Tutorial "Role-based Security with Forms Authentication" http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=433
8
5272
by: Tomasz | last post by:
Hello Developers! I have an interesting problem using my custom MembershipProvider, RoleProvider and Forms Authentication. Both MembershipProvider and RoleProvider require session state, where some very important context data are stored during the Session_Start event. My MembershipProvider and RoleProvider depend on this information. It seems that authentication process works with no problems.
2
2435
by: WT | last post by:
Hello, I tryed to fix a variable with the current authentication mode, I tryed to use Request.LogonUserIdentity AuthenticationType for this but when I traced with this code if (Request.LogonUserIdentity != null) { sb.AppendFormat("Request.LogonUserIdentity AuthenticationType: {0}<br />", Request.LogonUserIdentity.AuthenticationType); .....
1
2274
by: =?Utf-8?B?U3RlcGhhbmU=?= | last post by:
Hi, I have a problem with Integrated Windows Authentication on one server (Win Server 2003 SP2 IIS 6.0 ASP.Net 1.4). Let's say I want to disable anonymous connections to an admin directory, I usualy go in IIS, in the folder's properties and in the directory security tab. Then, I edit Authentication and Access control. I uncheck "allow anonymous connection" and I only keep the "Integrated Windows Authentication" box checked. When this...
0
7876
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8234
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6654
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2385
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.