• [转载]ASP.NET Web API author


    HTTP Basic 验证客户端的原理: 把HTTP头重的ContentType设置为:application/x-www-form-urlencoded 如果HTTP头没有Authorization,那么添加,并把这个设置为“Basic 用户名:密码”字符串组合的Base64编码。

    代码片段:

    复制代码
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method
    ="GET"; request.ContentType ="application/x-www-form-urlencoded"; request.Credentials = CredentialCache.DefaultCredentials;
    //获得用户名密码的Base64编码string code= Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "username", "password")));
    //添加Authorization到HTTP头request.Headers.Add("Authorization", "Basic "+ code);
    HttpWebResponse response
    = (HttpWebResponse)request.GetResponse(); StreamReader reader =new StreamReader(response.GetResponseStream());
    string content= reader.ReadToEnd();
    复制代码

    -----------------------------------------------------------

    访问需要HTTP Basic Authentication认证的资源的C#实现

     

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

    • 一是在请求头中添加Authorization: Authorization: "Basic 用户名和密码的base64加密字符串"
    • 二是在url中添加用户名和密码: http://userName:password@api.minicloud.com.cn/statuses/friends_timeline.xml

    下面来看下对于第一种在请求中添加Authorization头部的各种语言的实现代码。

    先看.NET的吧:

    复制代码
    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(new ASCIIEncoding().GetBytes(usernamePassword)));
    WebResponse wr
    = myReq.GetResponse(); Stream receiveStream = wr.GetResponseStream(); StreamReader reader =new StreamReader(receiveStream, Encoding.UTF8); string content = reader.ReadToEnd();

    -----------------------------------------------------

    using System; using System.Web.Http;
    using
    System.Net.Http;

    publicclassAuthAttribute:AuthorizeAttribute
    {
       
    publicoverridevoidOnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
       
    {
           
    HandleUnauthorizedRequest(actionContext);
       
    }

       
    protectedoverridevoidHandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
       
    {
           
    var response = actionContext.Request.CreateResponse(System.Net.HttpStatusCode.Redirect);
            response
    .Headers.Add("Location","http://www.google.com");
            actionContext
    .Response= response;
       
    }
    }

  • 相关阅读:
    unexpected inconsistency;run fsck manually esxi断电后虚拟机启动故障
    centos 安装mysql 5.7
    centos 7 卸载mysql
    centos7 在线安装mysql5.6,客户端远程连接mysql
    ubuntu 14.04配置ip和dns
    centos7 上搭建mqtt服务
    windows eclipse IDE打开当前类所在文件路径
    git 在非空文件夹clone新项目
    eclipse中java build path下 allow output folders for source folders 无法勾选,该如何解决 eclipse中java build path下 allow output folders for source folders 无法勾选,
    Eclipse Kepler中配置JadClipse
  • 原文地址:https://www.cnblogs.com/fx2008/p/2810434.html
Copyright © 2020-2023  润新知