Hey Programmers!!
I have implemented a WCF restful web service. Which is connected a database for CRUD operations. and I've created a console client to test this web service. Now I have to implement authentication on that web service. I want to use user name password authentication. There should be a sort of header which should be passed with every request from client end. And I want to compare it with user name password from database. Can anyone suggest me a simple mechanism for this purpose?? I've searched a lot on the internet but all in vain :'(
Loading
Mariee APosted Oct 30, 2014, 12:52 AM
We can create request at client side like this.
WebRequest req = WebRequest.Create(new Uri("http://localhost:9/Service.svc/method_Name/?parameter1=11"));
And to send user name and password with the request add header like this.
req.Method = "GET";
req.PreAuthenticate = true;
req.ContentType = @"application/xml; charset=utf-8";
req.Headers.Add(HttpRequestHeader.Authorization, string.Format("{0}:{1}", "userName", "password"));
And extract the header at service end like this and validate it according to your requirements.
WebOperationContext tx = WebOperationContext.Current;
string requestUri = tx.IncomingRequest.UriTemplateMatch.RequestUri.ToString();
string authHeader = tx.IncomingRequest.Headers[HttpRequestHeader.Authorization];
string[] keys = authHeader.Split(':');
string userName = keys[0];
string password = keys[1];
I hope it will help people out there :)
Vithal WadjePosted Sep 27, 2014, 1:24 PM