RestSharp HttpBasicAuthentication example

I have a WPF client using RestSharp services and WEB API. I am trying to use the HttpBasicAuthenticator as follows:

 RestRequest login = new RestRequest("/api/users/login", Method.POST); var authenticator = new HttpBasicAuthenticator("admin","22"); authenticator.Authenticate(Client, login); IRestResponse response = Client.Execute(login); 

The POST request is as follows:

 POST http://localhost/api/users/login HTTP/1.1 Authorization: Basic YWRtaW46MjI= Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml User-Agent: RestSharp/105.1.0.0 Host: dellnote:810 Content-Length: 0 Accept-Encoding: gzip, deflate Connection: Keep-Alive 
  • How to handle this field, Authorization: Basic YWRtaW46MjI= server-side? Do I get a username and password from this header?
  • How to return a security token from the server to the client and save it on the client side?

I need to get a simple authentication based on a security token, but I can not find an example that describes all aspects of this process. Can someone point me to some complete example that includes the client and server side (and uses RestSharp).

+4
source share
2 answers

From the RestSharp documentation:

 var client = new RestClient("http://example.com"); client.Authenticator = new SimpleAuthenticator("username", "foo", "password", "bar"); var request = new RestRequest("resource", Method.GET); client.Execute(request); 

The URL generated for this request will be http://example.com/resource?username=foo&password=bar

So, you get the password just like any other parameter (although, for security reasons, it is recommended to use the POST method, and then GET).

Regarding cookies, check this out: https://msdn.microsoft.com/en-us/library/system.windows.application.setcookie.aspx

https://msdn.microsoft.com/en-us/library/system.windows.application.getcookie.aspx

Hope this helps

+4
source

An alternative answer to your first question about extracting Auth header values ​​(server side) from How do I get basic authentication credentials from the header? :

 private UserLogin GetUserLoginCredentials() { HttpContext httpContext = HttpContext.Current; UserLogin userLogin; string authHeader = httpContext.Request.Headers["Authorization"]; if (authHeader != null && authHeader.StartsWith("Basic")) { string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim(); Encoding encoding = Encoding.GetEncoding("iso-8859-1"); string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword)); int seperatorIndex = usernamePassword.IndexOf(':'); userLogin = new UserLogin() { Username = usernamePassword.Substring(0, seperatorIndex), Password = usernamePassword.Substring(seperatorIndex + 1) }; } else { //Handle what happens if that isn't the case throw new Exception("The authorization header is either empty or isn't Basic."); } return userLogin; } 

Using this method can be:

 UserLogin userLogin = GetUserLoginCredentials(); 

Also see: A-WebAPI-Basic-Authentication-Authorization-Filter

An alternative answer to the second question about returning a token (Server Side):

 var httpResponseMessage = Request.CreateResponse(); TokenResponse tokenResponse; bool wasAbleToGetAccesToken = _identityServerHelper.TryGetAccessToken(userLogin.Username, userLogin.Password, platform, out tokenResponse); httpResponseMessage.StatusCode = wasAbleToGetAccesToken ? HttpStatusCode.OK : HttpStatusCode.Unauthorized; httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(tokenResponse), System.Text.Encoding.UTF8, "application/json"); return httpResponseMessage; 
0
source

All Articles