ERROR: The remote server returned an error: (401) Unauthorized.

L

Leonard Danao

I get this error when i run my code below

"An unhandled exception of type 'System.Net.WebException' occurred in
system.dll
Additional information: The remote server returned an error: (401)
Unauthorized."

I am attempting to access a secure https site which
1-the site gives you your own certificate to use to access the site
2-a uname/password challenge i am not sure if it is basic or digest but i
have tried both

I can manually enter the site using a browser
1- the site asks for a cert which you will need select before going onto the
username/password area no domain
2- uname/pwd entered the correct password
3- the site then sends you to the correct area for which you are authorized

manualy everythign works fine so i know the uname/pwd are correct
at 1 point i was getting a 403 forbidden error but adding line 12 solved
that

its not very complex and probably a newbie mistake and i know i did not add
error handling yet, i want it to error out on any problems,

I am trying to access the site to upload some HR information. and no ADP is
not supporting this

Thank you in advance for any help
Leonard Danao

Code:
1        ServicePointManager.CertificatePolicy = New
AcceptAllCertificatesPolicy
2
3       Dim ADPPayrollwebsite As String = "https://<Some ADP Site"

4        Dim userName As String = "<uname>"
5       Dim userPassword As String = "<pwd>"
6        Dim AuthDomain As String = "<domain>"
7        Dim OneCred As NetworkCredential
8        Dim CredCache As CredentialCache

9        ' The path to the certificate.
10       Dim Certificate As String = "<cer>"

11        ' Load the certificate into an X509Certificate object.
12        Dim cert As X509Certificate =
X509Certificate.CreateFromCertFile(Certificate)

13       Dim encoding As New ASCIIEncoding
14        Dim PostData As String = ""
15        Dim Data() As Byte = encoding.GetBytes(PostData)
16        Dim LoginReq As HttpWebRequest =
WebRequest.Create(ADPPayrollwebsite)

17        OneCred = New NetworkCredential(userName, userPassword) ',
AuthDomain)

18        Dim wrCache As CredentialCache = New CredentialCache
19       wrCache.Add(New Uri(ADPPayrollwebsite), "digest", New
NetworkCredential(userName, userPassword)) 'OneCred)

20       With LoginReq
21            .ClientCertificates.Add(cert)
22            .PreAuthenticate = False
23            .Credentials = wrCache
24            .KeepAlive = False
25           .Method = "POST"
26            .AllowAutoRedirect = True
27            .ContentType = "application/x-www-form-urlencoded"
28            .ContentLength = Data.Length
29        End With

30        Dim sendreq As Stream = LoginReq.GetRequestStream

31        sendreq.Write(Data, 0, Data.Length)
32        sendreq.Close()

33        Dim loginres As HttpWebResponse = LoginReq.GetResponse()
34        Dim sreader As StreamReader = New
StreamReader(loginres.GetResponseStream)
35        Dim HTML As String = sreader.ReadToEnd

36        txbTestArea.Text = ADPPayrollwebsite & vbCrLf  ' Site/Page
currently visiting

37        ' View headers for this connection
38        Dim i As Integer
39        While i < loginres.Headers.Count
40            txbTestArea.Text = txbTestArea.Text & loginres.Headers.Keys(i)
& " : " & loginres.Headers(i) & vbCrLf
41            i = i + 1
42        End While
43        'view the URI that responded to this request
44        txbTestArea.Text = txbTestArea.Text &
loginres.ResponseUri.ToString & vbCrLf
45        loginres.Close()

[\code]
 
L

Leonard Danao

also i would like to point out that line 14 has data, but i did not place it
there due to condifential information

Thanks



Leonard Danao said:
I get this error when i run my code below

"An unhandled exception of type 'System.Net.WebException' occurred in
system.dll
Additional information: The remote server returned an error: (401)
Unauthorized."

I am attempting to access a secure https site which
1-the site gives you your own certificate to use to access the site
2-a uname/password challenge i am not sure if it is basic or digest but i
have tried both

I can manually enter the site using a browser
1- the site asks for a cert which you will need select before going onto the
username/password area no domain
2- uname/pwd entered the correct password
3- the site then sends you to the correct area for which you are authorized

manualy everythign works fine so i know the uname/pwd are correct
at 1 point i was getting a 403 forbidden error but adding line 12 solved
that

its not very complex and probably a newbie mistake and i know i did not add
error handling yet, i want it to error out on any problems,

I am trying to access the site to upload some HR information. and no ADP is
not supporting this

Thank you in advance for any help
Leonard Danao

Code:
1        ServicePointManager.CertificatePolicy = New
AcceptAllCertificatesPolicy
2
3       Dim ADPPayrollwebsite As String = "https://<Some ADP Site"

4        Dim userName As String = "<uname>"
5       Dim userPassword As String = "<pwd>"
6        Dim AuthDomain As String = "<domain>"
7        Dim OneCred As NetworkCredential
8        Dim CredCache As CredentialCache

9        ' The path to the certificate.
10       Dim Certificate As String = "<cer>"

11        ' Load the certificate into an X509Certificate object.
12        Dim cert As X509Certificate =
X509Certificate.CreateFromCertFile(Certificate)

13       Dim encoding As New ASCIIEncoding
14        Dim PostData As String = ""
15        Dim Data() As Byte = encoding.GetBytes(PostData)
16        Dim LoginReq As HttpWebRequest =
WebRequest.Create(ADPPayrollwebsite)

17        OneCred = New NetworkCredential(userName, userPassword) ',
AuthDomain)

18        Dim wrCache As CredentialCache = New CredentialCache
19       wrCache.Add(New Uri(ADPPayrollwebsite), "digest", New
NetworkCredential(userName, userPassword)) 'OneCred)

20       With LoginReq
21            .ClientCertificates.Add(cert)
22            .PreAuthenticate = False
23            .Credentials = wrCache
24            .KeepAlive = False
25           .Method = "POST"
26            .AllowAutoRedirect = True
27            .ContentType = "application/x-www-form-urlencoded"
28            .ContentLength = Data.Length
29        End With

30        Dim sendreq As Stream = LoginReq.GetRequestStream

31        sendreq.Write(Data, 0, Data.Length)
32        sendreq.Close()

33        Dim loginres As HttpWebResponse = LoginReq.GetResponse()
34        Dim sreader As StreamReader = New
StreamReader(loginres.GetResponseStream)
35        Dim HTML As String = sreader.ReadToEnd

36        txbTestArea.Text = ADPPayrollwebsite & vbCrLf  ' Site/Page
currently visiting

37        ' View headers for this connection
38        Dim i As Integer
39        While i < loginres.Headers.Count
40            txbTestArea.Text = txbTestArea.Text & loginres.Headers.Keys(i)
& " : " & loginres.Headers(i) & vbCrLf
41            i = i + 1
42        End While
43        'view the URI that responded to this request
44        txbTestArea.Text = txbTestArea.Text &
loginres.ResponseUri.ToString & vbCrLf
45        loginres.Close()

[\code]
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top