Basic Authentication with asmx web service - asp.net

I'm trying to implement Basic Authorization for an ASMXweb service. I created the client as a service reference in VS2015. I'm using code in Asmx web service basic authentication as an example.
I'm entering login info in ClientCredentials as below
Dim svc As New WebServiceSoapClient()
svc.ClientCredentials.UserName.UserName = "userId"
svc.ClientCredentials.UserName.Password = "i2awTieS0mdO"
My problem is that in the Authorization HttpModule in the web service, these credentials are not being passed to module. Is there an alternate way to do this?

I found the parts answer at How to add HTTP Header to SOAP Client. I had to combine a couple of answers on that page to get it to work.
Dim svc As New WebServiceSoapClient()
Dim responseService As SoapResponseObject
Using (new OperationContextScope(svc.InnerChannel))
Dim auth = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("userId:i2awTieS0mdO"))
Dim requestMessage = New HttpRequestMessageProperty()
requestMessage.Headers("Authorization") = auth
OperationContext.Current.OutgoingMessageProperties(HttpRequestMessageProperty.Name) = requestMessage
dim aMessageHeader = MessageHeader.CreateHeader("Authorization", "http://tempuri.org", auth)
OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader)
responseService = svc.ListDistricts(requestService)
End Using
One key thing to be aware of is that the soap client call has to be inside the Using statement. In the above code, this is the next to last line.

Related

Identity Model invalid_client

I am just starting to learn about identity model and have been looking at the examples here - https://identitymodel.readthedocs.io/en/latest/client/token.html
I'm looking at the simplest example - Requesting a token using the client_credentials Grant Type - and using the example code I have put together some simple vb.net to test and experiment with.
The problem I am getting is that I only ever get back an invalid_client error message.
I'm sure it is something obvious that I am missing but if someone could point me i the right direction that would be immensely helpful.
Dim request As New ClientCredentialsTokenRequest
request.Address = "https://demo.identityserver.io/connect/token"
request.ClientId = "client"
request.ClientSecret = "secret"
request.Scope = "api1"
Dim client As New HttpClient
Dim response As TokenResponse = Await client.RequestClientCredentialsTokenAsync(request)
As indicated in the reply by #abdusco the correct code should look like
Dim request As New ClientCredentialsTokenRequest
request.Address = "https://demo.identityserver.io/connect/token"
request.ClientId = "m2m"
request.ClientSecret = "secret"
request.Scope = "api"
Dim client As New HttpClient
Dim response As TokenResponse = Await client.RequestClientCredentialsTokenAsync(request)
Thank you for the help.

WooCommerce REST API The remote server returned an error: (401) Unauthorized VB.NET

My site (https://www.trs.is) recently switched to https and now I am getting an Unauthorized error when I try to get all products from my WooCommerce store. I have tried many things with the WebClient() to use basic authentication but I always get this error.
I have also tested this with Postman from Google with basic auth and using the client key and client secret but the error is always the same.
Scratching my head here and after hours of searching I can not see why the this is happening.
Here is my latest code (actual client keys are removed)
Dim url As String = "https://www.trs.is/wc-api/v3/products"
'Dim client As New WebClient()
Dim userName As [String] = "myclientkey"
Dim passWord As [String] = "myclientsecret"
'client.Credentials = New System.Net.NetworkCredential(userName, passWord)
Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + passWord))
wc.Headers(HttpRequestHeader.Authorization) = Convert.ToString("Basic") & credentials
Dim result = wc.DownloadString(url)
Any help guys ?
Try using wc.UseDefaultCredentials = True and post your API keys instead.

How to pass data to a WebApi Controller from a console application?

Using this code I am not able to post data to an api (asp.net MVC latest version).
What could be the problem?
Should I set specific headers?
Using client As New Net.WebClient
Dim reqparm As New Specialized.NameValueCollection
reqparm.Add("DeviceId", "devicetest")
reqparm.Add("Message", "messagetest")
Dim responsebytes = client.UploadValues("http://xxx.xxx.com/api/notification", "POST", reqparm)
Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)
End Using

How to redirect to home page of Pentaho after basic authentication in .NET?

I try to use basic authentication in VB.NET side to login Pentaho
I use the default account of Pentaho for testing
username: joe
password: password
I have following code in VB.NET for basic authentication to Pentaho
Dim request = WebRequest.Create("http://x.x.x.x:8080/pentaho/Home")
Dim authInfo As String = Convert.ToString(userName) & ":" & Convert.ToString(userPassword)
authInfo = Convert.ToBase64String(Encoding.[Default].GetBytes(authInfo))
request.Headers("Authorization") = "Basic " & authInfo
Dim response As WebResponse = request.GetResponse()
After ran the request.GetResponse() can get the successful result. So I think the Pentaho login and authentication successfully
But when I go to http://x.x.x.x:8080/pentaho/Home Pentaho still prompt to Login Pageā€¦
Do You know What wrong of my code?
Thanks in advance!!
Not sure what you're trying to do here. Where are you going to pentaho/Home. In your code ?
If you want to programatically call a certain URL in Pentaho, it's probably easier to append the credentials to the query string.
You can use Basic Authentication method, I already answered this question in bellow post.
enter link description here
Eg-
WebClient webClient = new System.Net.WebClient();
Uri uri = new Uri("http://serverDomain:8080/pentaho/Home");
//Give user name and password here
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes("username:password");
var encodedString = System.Convert.ToBase64String(plainTextBytes);
webClient.Headers["Authorization"] = "Basic " + encodedString;
webClient.Encoding = Encoding.UTF8;
App.WindowManager.ConsoleWrite(uri.ToString());
webClient.UploadStringAsync(uri, "POST", "");

How to get the binding address from a WSDL file

I generated a proxy class given a URL to a WSDL.
I need to let the end-user change the service's URL to his specific URL, like this:
ServiceProxy.Url = [URL set by end-user];
The issue is that this URL should not point to the WSDL, it should be the binding address which is found within the WSDL (wsdl:service -> wsdl:port -> wsdl:address) (this is a SAP web service, I understand that is why I must use the binding address).
I am thinking of using the XDocument class to get that value, but I am wondering if there is any "built-in" functionality in WCF or web services to get the binding address. Thank you.
I did a small function in VB.NET (sorry!) based on code at Parse Complex WSDL Parameter Information . Hope it helps.
Public Function GetURLFromWSDL(ByVal wsdl As String) As String
Dim request As HttpWebRequest = WebRequest.Create(wsdl)
request.ContentType = "text/xml;charset=""utf-8"""
request.Method = "GET"
request.Accept = "text/xml"
Using response As WebResponse = request.GetResponse()
Using stream As Stream = response.GetResponseStream()
Dim service As ServiceDescription = ServiceDescription.Read(stream)
Dim binding As SoapAddressBinding = service.Services(0).Ports(0).Extensions(0)
Return binding.Location
End Using
End Using
End Function

Resources