how to send the header correctly with c# for authentication

RESTful knowledge exchange

how to send the header correctly with c# for authentication

Veeam Logoby dannordquist » Thu Oct 23, 2014 3:49 pm

I've tried many variations of code but this is the gist:

Code: Select all
HttpWebRequest httpWReq = (HttpWebRequest)HttpWebRequest.Create("http://mysite:9399/api/sessionMngr/");
                httpWReq.Method = "POST";

                ASCIIEncoding encode = new ASCIIEncoding();
                string _auth = string.Format("{0}:{1}", "user", "pass");
                string _enc = Convert.ToBase64String(encode.GetBytes(_auth));
                string _cred = string.Format("{0} {1}", "Basic", _enc);
                byte[] data = encode.GetBytes(_cred);
                httpWReq.ContentLength = _cred.Length;
                httpWReq.Headers[HttpRequestHeader.Authorization] = _cred;
               
                string responseString = new StreamReader(httpWReq.GetResponse().GetResponseStream()).ReadToEnd();

                Console.WriteLine(responseString);

I get a 500 error from the server.

Any ideas?

Thanks!
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

why is the moderator deleting my posts?

Veeam Logoby dannordquist » Thu Oct 23, 2014 4:21 pm

I've replied to several posts with related comments and they are getting deleted.

Does Veeam not want this to be successful or just want to frustrate everyone who's looking for answers?
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

Re: how to send the header correctly with c# for authenticat

Veeam Logoby dannordquist » Thu Oct 23, 2014 4:33 pm

Here's the header text being sent to the server, which is exactly the correct format specified in the documentation:

Content-Type: text/xml
Authorization: Basic dmVlYW06VjMzYU0yMDEx

Still get a 500 error though...
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

Re: how to send the header correctly with c# for authenticat

Veeam Logoby v.Eremin » Thu Oct 23, 2014 4:33 pm

Your post were deleted, because they were the same. Meanwhile, your original topic has been preserved. Please, don't overfill multiple topics with the same issue, and keep posting in this thread.

Thank you for your understanding.
v.Eremin
Veeam Software
 
Posts: 13296
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: how to send the header correctly with c# for authenticat

Veeam Logoby dannordquist » Thu Oct 23, 2014 4:35 pm

I posted on several threads because people will get notification of the post, and if they found an answer it might prompt them to post it.

Posting a new thread does not notify people who have the same question.

Thank you for making this more difficult.
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

Re: how to send the header correctly with c# for authenticat

Veeam Logoby poulpreben » Fri Oct 24, 2014 2:22 pm

Hi Dan,

You probably want to have a look at this thread over at StackOverflow: C# webrequest authentication

I would love to try, but I never did much C# programming, so please post your findings in this thread!

Thanks,
Preben
poulpreben
Expert
 
Posts: 935
Liked: 398 times
Joined: Mon Jul 23, 2012 8:16 am
Full Name: Preben Berg

Re: how to send the header correctly with c# for authenticat

Veeam Logoby dannordquist » Fri Oct 24, 2014 2:31 pm

Thanks poulpreben. I've found that there's nothing wrong with my code, however. It works just fine, but the server returns a 500 error because we don't have the Enterprise Plus license. It doesn't indicate anything about licensing or why it got the 500 error, so it was difficult to find that it was just a licensing issue (wasted two days thinking it was a code issue).

Looks like it's a bit costly for the Plus license so if we can't get budget for it I'll end up figuring out the DB structure and parsing the Database instead. Won't be able to do anything but read information but better than nothing.
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

Re: how to send the header correctly with c# for authenticat

Veeam Logoby v.Eremin » Fri Oct 24, 2014 2:36 pm

What about using PowerShell, instead of parsing DB? Not only will be more convenient, from my perspective, but also it will not be read-only stuff, as changes are totally possible via PS. Thanks.
v.Eremin
Veeam Software
 
Posts: 13296
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: how to send the header correctly with c# for authenticat

Veeam Logoby dannordquist » Fri Oct 24, 2014 2:42 pm

Thank you v.Eremin. Would you mind posting some example PS code to get me started on the right path? Just login and reading data (current jobs with failure/warnings and job duration would be awesome) will get me started and then I hope to find information on adding commands and/or changes after proving the functionality.
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

Re: how to send the header correctly with c# for authenticat

Veeam Logoby v.Eremin » Fri Oct 24, 2014 3:01 pm

The best idea would be to check both PowerShell subforum and our online Help Center.

As to the script examples, the following ones should be helpful:

Get all jobs that failed during the last run

Code: Select all
Asnp VeeamPSSnapin
Get-VBRJob | where {$_.GetLastResult() -eq "Failed"}


Select all jobs along with the start and end time of their latest sessions:

Code: Select all
Asnp VeeamPSSnapin
Get-VBRJob | select name, {$_.Findlastsession().creationTime}, {$_.Findlastsession().EndTime}


If you want to continue discussion regarding PS, please, create a separate topic on the said subforum.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13296
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: how to send the header correctly with c# for authenticat

Veeam Logoby dannordquist » Fri Oct 24, 2014 3:29 pm

Excellent! thank you!
dannordquist
Enthusiast
 
Posts: 27
Liked: 2 times
Joined: Thu Oct 23, 2014 3:39 pm
Full Name: Dan Nordquist

Re: how to send the header correctly with c# for authenticat

Veeam Logoby v.Eremin » Mon Oct 27, 2014 7:17 am

You're welcome. Should other questions arise, feel free to contact us. Thanks.
v.Eremin
Veeam Software
 
Posts: 13296
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to RESTful API



Who is online

Users browsing this forum: No registered users and 2 guests