Solved

ASP.NET/VB: Username and password with HttpWebRequest

Posted on 2013-12-17
1
977 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now