gpt4 book ai didi

c# - 单个 NetworkCredential 实例是否仅适用于单个 URI

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:38 26 4
gpt4 key购买 nike

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

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

然而,在组合中加入一些重定向会导致问题。我现在正在返回 401。这是否意味着我的凭据对象仅绑定(bind)到特定的 URI?那发生在哪里?我在 NetworkCredential 类中的任何地方都看不到这种情况。

也许设置 HttpWebRequest.Credentials 的过程将该凭据与在 HttpWebRequest 的构造函数中指定的 URI 绑定(bind)...

如果是这种情况,如何解决这个问题?

仅供引用,身份验证方案是 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;
}

最佳答案

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

关于c# - 单个 NetworkCredential 实例是否仅适用于单个 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4905215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com