关于NetworkCredential类,这很可能是一个非常幼稚的问题。请原谅我的经验不足...

我使用用户名,密码和域创建一个NetworkCredential。然后,我使用HttpWebRequest与URI进行交互。通过设置HttpWebRequest.Credentials,NetworkCredential用于对此URI进行身份验证。这一直很好。

但是,将一些重定向引入到混合中会导致问题。我现在要退回401。这是否意味着我的凭证对象仅绑定到特定的URI?哪里发生的?我在NetworkCredential类的任何地方都没有看到这种情况发生。

也许设置HttpWebRequest.Credentials的过程会将凭证与仅在HttpWebRequest的构造函数中指定的URI关联起来...

在这种情况下,如何解决这个问题?

仅供参考,身份验证方案是Kerberos。除了用户可能使用本地用户帐户(不在Kerberos中)之外,我将使用DefaultCredentials。但是,用户将在程序中输入有效的Kerberos帐户(使用我编写的登录对话框)。

我已经编写了一种方法,该方法将手动遍历重定向以创建一个NetworkCredential,该NetworkCredential指向我要使用的相同主机名(但不完全是URI)。代码如下:

    /// <summary>
    /// Get a NetworkCredentials instance associated with location.
    /// </summary>
    /// <param name="location">A URI to test user credentials against.</param>
    /// <param name="userName">Username.</param>
    /// <param name="password">Password.</param>
    /// <param name="domain">Domain.</param>
    /// <param name="throwExceptions">Throws exceptions on error if true.</param>
    /// <returns>On success a NetworkCredential instance is returned.  If throwExceptions equals
    /// true all exceptions will propogate up the stack, otherwise null is returned.</returns>
    public static NetworkCredential GetCredential(Uri location, string userName,
        SecureString password, string domain, bool throwExceptions = true)
    {
        NetworkCredential ret = null;
        try
        {
            Uri uri = location;
            bool redirected = false;
            do
            {
                HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;

                ret = new NetworkCredential(userName, password, domain);
                request.UseDefaultCredentials = false;
                request.Credentials = ret;

                request.AllowAutoRedirect = false;
                HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
                if (resp.StatusCode == HttpStatusCode.Redirect)
                {
                    uri = new Uri(resp.GetResponseHeader("Location"));
                    redirected = true;
                }
                else
                {
                    redirected = false;
                }
            } while (redirected);
        }
        catch
        {
            if (throwExceptions)
            {
                throw;
            }
            ret = null;
        }
        return ret;
    }

最佳答案

       // NetworkCredential stores authentication to a single internet resource
       // supplies credentials in password-based authentication schemes such as basic, digest, NTLM, and Kerberos.
       NetworkCredential networkCredential = new NetworkCredential("username", "password");

        WebRequest webRequest = HttpWebRequest.Create("www.foobar.com");
        webRequest.Credentials = networkCredential;

        // to use the same credential for multiple internet resources...
        CredentialCache credentialCache = new CredentialCache();
        credentialCache.Add(new Uri("www.foobar.com"), "Basic", networkCredential);
        credentialCache.Add(new Uri("www.example.com"), "Digest", networkCredential);

        // now based on the uri and the authetication, GetCredential method would return the
        // appropriate credentials
        webRequest.Credentials = credentialCache;


herehere的更多信息

10-07 21:41