有没有一个单一的NetworkCredential实例,只适用于单一的URI适用于、实例、NetworkCredential、URI

2023-09-06 07:12:52 作者:打不倒的小乖兽

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

我创建的NetworkCredential使用用户名,密码和域名。然后我用HttpWebRequest的同一个URI进行交互。该的NetworkCredential是通过设定HttpWebRequest.Credentials来验证此URI。这一直是工作的罚款。

抛出一些重定向到组合已经但是引起的问题。我现在得到401的返回。这是否意味着我的凭据对象只针对某一特定URI?在哪里发生的?我没有看到任何地方NetworkCredential类这种情况的发生。

设置HttpWebRequest.Credentials关系的凭证只是HttpWebRequest的...

的构造函数中指定的URI

也许过程

如果是这样的话,一个人如何解决这个问题得到什么呢?

仅供参考,认证方案是Kerberos的。我会使用DefaultCredentials用户除了可以使用本地用户帐户(而不是Kerberos中)。用户将然而进入一个有效的Kerberos帐户到程序(使用一个登录对话框我写)。

我已经写了将通过重定向走路手动创建的NetworkCredential指向相同的主机名(但不是精确URI)我飞机的使用方法。低于code:

  ///<总结>
    ///获取与位置相关联的NetworkCredentials实例。
    ///< /总结>
    ///< PARAM NAME =位置>一种URI来测试对用户凭据和LT; /参数>
    ///< PARAM NAME =username的>用户名< /参数>
    ///< PARAM NAME =密码>密码< /参数>
    ///< PARAM NAME =域>域名< /参数>
    ///< PARAM NAME =throwExceptions>抛出的异常的错误,如果真LT; /参数>
    ///<返回>在成功的NetworkCredential实例返回。如果throwExceptions等于
    ///真正的所有异常将propogate堆栈,否则返回null< /回报>
    公共静态的NetworkCredential GetCredential(URI位置,用户名字符串,
        SecureString的密码,串域,布尔throwExceptions =真)
    {
        的NetworkCredential RET = NULL;
        尝试
        {
            开放的我们的uri =位置;
            布尔重定向= FALSE;
            做
            {
                HttpWebRequest的请求= WebRequest.Create(URI),为的HttpWebRequest;

                RET =新的NetworkCredential(用户名,密码,域);
                request.UseDefaultCredentials = FALSE;
                request.Credentials = RET;

                request.AllowAutoRedirect = FALSE;
                HttpWebResponse RESP = request.GetResponse()作为HttpWebResponse;
                如果(resp.Status code ==的HTTPStatus code.Redirect)
                {
                    URI =新的URI(resp.GetResponseHeader(位置));
                    重定向= TRUE;
                }
                其他
                {
                    重定向= FALSE;
                }
            }而(重定向);
        }
        抓住
        {
            如果(throwExceptions)
            {
                扔;
            }
            RET = NULL;
        }
        返回RET;
    }
 
没有Windows Media Player Network Sharing Service

解决方案

  //的NetworkCredential店认证单一的互联网资源
       在基于口令的认证方案//用品凭据,如基本,摘要,NTLM和Kerberos。
       的NetworkCredential的NetworkCredential =新的NetworkCredential(用户名,密码);

        WebRequest类的WebRequest = HttpWebRequest.Create(www.foobar.com);
        webRequest.Credentials =的NetworkCredential;

        //使用相同的凭据多个网络资源...
        CredentialCache credentialCache =新CredentialCache();
        credentialCache.Add(新的URI(www.foobar.com),基本,的NetworkCredential);
        credentialCache.Add(新的URI(www.example.com),文摘,的NetworkCredential);

        //现在基于URI和authetication,getCredential方法将返回
        //相应的凭证
        webRequest.Credentials = credentialCache;
 

更多的此信息和这里

This will quite possibly be a very naive question about the NetworkCredential class. Excuse my inexperience please...

I create a NetworkCredential using a username, password, and domain. I then use HttpWebRequest to interact with a URI. The NetworkCredential is used to authenticate against this URI by setting HttpWebRequest.Credentials. This has been working fine.

Throwing some redirection into the mix has caused problems however. I'm now getting 401's returned. Does this mean that my credential object is tied only to a specific URI? Where does that happen? I don't see anywhere in the NetworkCredential class that this happens.

Maybe the process of setting HttpWebRequest.Credentials ties that credential to just the URI that was specified in the constructor of HttpWebRequest...

If this is the case, how does one get around this problem?

FYI, the authentication scheme is Kerberos. I would use the DefaultCredentials except the user may be using a local user account (not in Kerberos). The user will however be entering a valid Kerberos account into the program (using a login dialog I've written).

I've written a method that will walk through the redirects manually to create a NetworkCredential that points to the same hostname (but not exact URI) I plane to use. Code below:

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

More info at here and here