C#调用HTTP Basic Authentication安全认证的Webservice

1、Net的方式

要在发送请求的时候添加HTTP Basic Authentication认证信息到请求中,有两种方法:

一是在请求头中添加Authorization:
Authorization: "Basic 用户名和密码的base64加密字符串"
二是在url中添加用户名和密码:
http://userName:password@api.minicloud.com.cn/statuses/friends_timeline.xml
下面来看下对于第一种在请求中添加Authorization头部的各种语言的实现代码。

string username="username";
string password="password";
//注意这里的格式哦,为 "username:password"
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(newASCIIEncoding().GetBytes(usernamePassword))); 


WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
你当然也可以使用HttpWebRequest或者其他的类来发送请求。

2、源码实现: 

    WebReference.ExecutingServiceImplService client = new WebReference.ExecutingServiceImplService();
    string uri = "http://10.8.200.888:8087/MM-MES-QQ/services/ExecutingService?wsdl";
    CredentialCache mycache = new CredentialCache();
    //设置访问接口 Preemptive
    mycache.Add(new Uri(uri), "Basic", new NetworkCredential("userA001", "pass123"));
    string usernamePassword = "userA001" + ":" + "pass@123";
    client.Credentials = mycache;
    client.PreAuthenticate = true;     //设置预验证

    WebRequest req = WebRequest.Create(new Uri(uri));
    req.Credentials = mycache;
    req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

    string soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.cxf.core.bgy.com/\">" + "<soapenv:Header/>" +
                     "<soapenv:Body>" +
                         "<ser:execute>" +
                         "<pRequest>" +
                             "<plant>" + "123" + "</plant>" +
                             "<data>" + "123" + "</data>" +
                             "<serviceCode>" + "123" + "</serviceCode>" +
                         "</pRequest>" +
                         "</ser:execute>" +
                     "</soapenv:Body>" +
                 "</soapenv:Envelope>";

    //将SOAP字符串信息转换成Byte数组,用于后面的流传输
    byte[] bytData = Encoding.UTF8.GetBytes(soap.ToString());
    req.Method = "POST";//POST方式传输
    req.ContentType = "text/xml; charset=utf-8";//传输内容类型及编码格式
    req.ContentLength = bytData.Length;//传输内容长
    Stream newStream = req.GetRequestStream();
    //将数据写入该流
    newStream.Write(bytData, 0, bytData.Length);//写入参数
    newStream.Close();
    WebResponse wr = req.GetResponse();
    Stream receiveStream = wr.GetResponseStream();
    StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
    string content = reader.ReadToEnd();

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: Prometheus自带的Basic Authentication是一种简单的认证方式,它需要使用用户名和密码进行认证。下面是一个示例: 首先,需要在Prometheus配置文件中设置认证信息,例如: ``` # my prometheus.yml global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' # 配置认证信息 basic_auth: username: "admin" password: "secret" static_configs: - targets: ['localhost:9090'] ``` 然后,启动Prometheus服务器并访问其Web界面。在访问时,浏览器会弹出认证对话框,要求输入用户名和密码。在本例中,用户名为“admin”,密码为“secret”。如果输入正确,则可以成功登录并访问Prometheus Web界面。 ### 回答2: Prometheus是一款开源的监控系统,支持多种认证方式,包括Basic AuthenticationBasic Authentication基于用户和密码的方式进行登录认证。以下是一个使用Basic Authentication的Prometheus登录认证示例: 1. 首先,在Prometheus的配置文件prometheus.yml中添加以下配置: ``` - job_name: 'prometheus' scheme: http basic_auth: username: your_username password: your_password static_configs: - targets: ['localhost:9090'] ``` 在上述示例中,username和password分别填写你自己设置的用户名和密码。 2. 保存并重启Prometheus服务使配置生效。 3. 使用浏览器或其他HTTP请求工具进行访问时,在请求的URL中添加用户名和密码,例如: ``` http://localhost:9090/graph?username=your_username&password=your_password ``` 请记住,Basic Authentication是一种基本的认证方式,通过明文传输用户名和密码进行认证,并不是一种安全认证方法。因此,在生产环境中,建议考虑使用更安全认证方式,如OAuth认证或使用反向代理服务器进行认证。 ### 回答3: Prometheus是一个开源的监控和警报系统,它可以通过Basic Authentication来进行登录认证Basic Authentication是一种简单的HTTP认证方式,它通过在每个请求的Header中包含用户名和密码来进行身份验证。 下面是一个使用Prometheus自带的Basic Authentication进行登录认证的示例: 1. 首先,在Prometheus的配置文件`prometheus.yml`中添加以下内容: ```yaml basic_auth_users: - username: admin password: password123 ``` 该示例中,我们创建了一个用户名为`admin`,密码为`password123`的用户。 2. 保存配置文件并重新启动Prometheus服务。 3. 打开浏览器,输入Prometheus的URL(例如:http://localhost:9090)。 4. 浏览器将弹出一个身份验证对话框,要求输入用户名和密码。 5. 输入之前在配置文件中设置的用户名和密码,点击“登录”按钮。 6. 登录成功后,将跳转到Prometheus的主页面,可以开始使用各种监控和查询功能。 通过以上步骤,我们成功地使用Prometheus自带的Basic Authentication进行了登录认证。当访问Prometheus时,浏览器会提示输入用户名和密码,并且只有通过身份验证的用户才能访问和使用Prometheus的功能。 需要注意的是,Basic Authentication是一种简单的认证方式,它以明文形式传输用户名和密码,安全性较低,不适合在非受信任的环境中使用。在实际生产环境中,推荐使用更安全认证方式,如OAuth2、LDAP等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兰舟轻帆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值