【问题标题】:HttpListener: how to get http user and password?HttpListener:如何获取http用户和密码?
【发布时间】:2010-11-11 22:47:59
【问题描述】:

我在这里遇到了一个问题,使用 HttpListener。

当表单请求时

http://user:password@example.com/

制作完成,如何获取用户名和密码? HttpWebRequest 有一个 Credentials 属性,但是 HttpListenerRequest 没有它,而且我在它的任何属性中都没有找到用户名。

感谢您的帮助。

【问题讨论】:

    标签: c# passwords httplistener


    【解决方案1】:

    您尝试做的是通过 HTTP 基本身份验证传递凭据,我不确定 HttpListener 是否支持 username:password 语法,但如果是,您需要指定您接受基本身份验证首先。

    HttpListener listener = new HttpListener();
    listener.Prefixes.Add(uriPrefix);
    listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
    listener.Start();
    

    收到请求后,您可以提取用户名和密码:

    HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
    Console.WriteLine(identity.Name);
    Console.WriteLine(identity.Password);
    

    Here's a full explanation 可与 HttpListener 一起使用的所有受支持的身份验证方法。

    【讨论】:

    • 对不起,我说“如果 HttpListener 支持 username:password 语法,我就不支持”,但当然是客户端将其转换为“WWW-Authenticate: basic”标头,所以只有客户支持它才重要。我相信最近 IE 已经放弃了对它的支持。
    【解决方案2】:

    获取Authorization 标头。其格式如下

    Authorization: <Type> <Base64-encoded-Username/Password-Pair>
    

    例子:

    Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
    

    用户名和密码用冒号分隔(在本例中为Aladdin:open sesame),然后是 B64 编码。

    【讨论】:

      【解决方案3】:

      您需要先启用基本身份验证:

      listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
      

      然后在您的 ProcessRequest 方法中,您可以获得用户名和密码:

      if (context.User.Identity.IsAuthenticated)
      {
          var identity = (HttpListenerBasicIdentity)context.User.Identity;
          Console.WriteLine(identity.Name);
          Console.WriteLine(identity.Password);
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-15
        • 2014-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多