Click here to Skip to main content
11,577,258 members (57,717 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi. I am new in both curl and ASP.NET. I am trying to do the curl auth login in ASP.NET.

For curl auth login, it will look like this
<pre lang="xml">curl -k https://localhost:8089/services/auth/login \
-d username=admin \
-d password=pass</pre>

So in ASP.NET, i try out this:
string url = "https://localhost:8089/services/auth/login";
WebRequest myReq = WebRequest.Create(url);
 
string username = "admin";
string password = "pass";
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username+"&"+password)));
 
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
 
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Console.WriteLine(content);
Console.ReadLine();

However, I am not sure my way in ASP.NET is the correct or vice versa since it gives me a 400 Bad Request.

By the way, without
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });

it will give me underlying SSL connection is not trusty.

Hope that someone may know the way to do this right. Thanks!
Posted 22-Oct-12 15:22pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 155
1 Afzaal Ahmad Zeeshan 125
2 OriginalGriff 115
3 Richard MacCutchan 100
4 Abhinav S 90
0 OriginalGriff 820
1 Sergey Alexandrovich Kryukov 676
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 375


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 22 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100