【问题标题】:Does a single NetworkCredential instance apply to only a single URI单个 NetworkCredential 实例是否仅适用于单个 URI
【发布时间】:2011-06-21 18:30:04
【问题描述】:

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

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

然而,在混合中加入一些重定向会导致问题。我现在收到了 401 的退货。这是否意味着我的凭证对象仅与特定的 URI 相关联?发生在哪里?我在 NetworkCredential 类的任何地方都看不到发生这种情况。

也许设置 HttpWebRequest.Credentials 的过程将该凭据与 HttpWebRequest 的构造函数中指定的 URI 联系起来......

如果是这样,如何解决这个问题?

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

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

    /// <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;
    }

【问题讨论】:

    标签: c# .net authentication redirect


    【解决方案1】:
           // 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

    【讨论】:

    • 我想知道是否可能是这种情况。我想现在我必须想出一个策略来构建一个 CredentialCache,因为我点击了每个重定向。现在使用自动重定向。叹息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多