HttpBasicAuthentication

HttpBasicAuthentication

本文介绍了RestSharp HttpBasicAuthentication-示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用RestSharp和WEB API服务的WPF客户端.我尝试如下使用HttpBasicAuthenticator:

I have a WPF client using RestSharp and WEB API Service. I try to use 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);

POST请求看起来像这样:

The POST request looks like this:

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
  1. 如何在服务器端处理此字段Authorization: Basic YWRtaW46MjI=?我可以从此标头中获取用户名和密码吗?
  2. 如何将安全令牌从服务器返回到客户端并将其保存在客户端?
  1. How do I process this field, Authorization: Basic YWRtaW46MjI= on the server side? Do I get username and password from this header?
  2. How do I return security token from server to client and save it on the client side?

我需要获得基于安全令牌的简单身份验证,但是找不到描述此过程所有方面的示例.有人可以指出一些完整的示例,包括客户端和服务器端(并使用RestSharp).

I need to get simple authentication based on security token but cannot find example that describes all sides of this process. Can someone point me to some full example that includes client and server side (and uses RestSharp).

推荐答案

new SimpleAuthenticator("username", username, "password", password)与我一起工作.

以下内容有效:

var client = new RestClient("http://example.com");
client.Authenticator = new HttpBasicAuthenticator(userName, password);

var request = new RestRequest("resource", Method.GET);
client.Execute(request);

这篇关于RestSharp HttpBasicAuthentication-示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:19