【问题标题】:HttpWebRequest Authentication not workingHttpWebRequest 身份验证不起作用
【发布时间】:2015-07-08 00:22:51
【问题描述】:

好的,所以我正在编写一个程序来访问 RESTful 服务,但我首先必须为网站提供身份验证。我有有效的凭据,我的整个代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;

namespace AccessAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            string uri = "http://domainName/";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            string auth = CreateAuthorization("http://domainName/", "my\\username", "password");
            request.Headers["Authorization"] = "Basic " + auth;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            Console.WriteLine(reader.ReadToEnd());
        }

        static string CreateAuthorization(string realm, string userName, string password)
        {
            string auth = ((realm != null) && (realm.Length > 0) ?
        realm + @"\" : "") + userName + ":" + password;
            auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
            return auth;
        }
    }
}

我使用其他论坛上发布的一些解决方案已经走到了这一步。虽然这似乎对某些人有用,但对我来说却不行。首先,我知道凭据是准确的。我可以通过网络浏览器输入它们,并且它们的行为与应有的一样。另外,我确保其中的任何“\”(用户名中有一个)在代码中用“\\”表示。但是,每次运行此程序时,我都会不断返回“远程服务器返回错误:(401)未经授权。” 谁能帮我解决这个问题?感谢您的宝贵时间!

【问题讨论】:

  • REST 服务是否使用基本身份验证?一个非常常见的做法是拥有一个名为“login”或“auth”或其他任何名称的资源,将凭据传递给它,获取授权令牌然后使用该令牌,您需要阅读服务文档。查看哪种授权机制使用。

标签: c# authentication web passwords httpwebrequest


【解决方案1】:

从上面的代码中,当“auth”字符串组成时,auth 字符串将如下所示:“http://domainName/:my\username:password”

auth 字符串的格式应该是:domainName\username:password(域名是可选的)。

【讨论】:

    【解决方案2】:

    感谢 Gusman 和 Prashant,你们的 cmets 帮助我朝着正确的方向前进并最终取得成功。所以 Gusman,你是对的,这不仅仅是任何身份验证标准,它使用的是 Window 的 IIS 服务。谷歌搜索如何使用 HttpWebRequest 实现这一点让我构建了这个:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Net;
    using System.IO;
    
    namespace AccessAPI
    {
        class Program
        {
            static void Main(string[] args)
            {
                string uri = "http://domain.name/";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "GET";
                request.UseDefaultCredentials = false;
                request.PreAuthenticate = true;
                request.Credentials = new NetworkCredential("username", "password");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                Console.WriteLine(reader.ReadToEnd());
            }
        }
    }
    

    现在为什么这行得通,而其他尝试却没有,我不知道。但这一切的症结似乎只是基于这条线

    request.Credentials = new NetworkCredential("username", "password");
    

    顺便说一句,Prashant,当我不使用整个 http://domain.name/ 时,就像你说的那样。 好吧,希望如果其他人遇到同样的问题,他们将能够找到这个线程并解决问题。

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 1970-01-01
      • 2012-04-29
      • 2012-03-18
      • 2011-07-05
      • 2013-12-02
      • 2013-06-29
      相关资源
      最近更新 更多