Solved

ASP.NET/VB: Username and password with HttpWebRequest

Posted on 2013-12-17
1
1,134 Views
Last Modified: 2013-12-17
Using ASP.NET/VB, how can I provide a username and password for basic authentication?
<%@ Page Language="VB" %>
<%@Import namespace="System"%>
<%@Import namespace="System.Web"%>
<%@Import namespace="System.Net"%>
<%@Import namespace="System.Xml"%>
<%@Import namespace="System.IO"%>
<%@Import namespace="System.Text"%>
<%

Dim postString as String = "{}"

Dim webRequest As HttpWebRequest = TryCast(WebRequest.Create("http://example.com/"), HttpWebRequest)
webRequest.USERNAME = "usernamex"
webRequest.PASSWORD = "passwordx"
webRequest.Method = "POST"
webRequest.ContentType = "application/json"
webRequest.ContentLength = postString.Length
webRequest.Accept = "application/json"
Dim requestWriter As New StreamWriter(webRequest.GetRequestStream())
requestWriter.Write(postString)
requestWriter.Close()

%>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39724922
Try removing these two lines:
webRequest.USERNAME = "usernamex"
webRequest.PASSWORD = "passwordx"

Open in new window

And replacing them with:
Dim auth As String = "usernamex" & ":" & "passwordx"
auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth))
webRequest.Headers("Authorization") = "Basic " & auth

Open in new window

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 22
Get month and date in a format 4 32
Access Schema 6 26
Can Angular be used with classic asp.net? 3 18
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question