【问题标题】:Get current request's credentials for using in WebRequest获取当前请求的凭据以在 WebRequest 中使用
【发布时间】:2008-12-10 09:34:38
【问题描述】:

访问我的网站时,用户必须输入他的凭据。它们基本上是普通的目录访问凭据。 在某个时刻,我通过调用检查他们想要下载的某个文件是否存在

WebRequest req = HttpWebRequest.Create(checkUri.AbsoluteUri);
WebResponse res = req.GetResponse();

虽然我可以从浏览器访问 checkUri,但在执行上述检查时我得到了 401。我想我必须设置

req.Credentials

但我不知道当前凭据存储在哪里...

有什么想法吗?

--更新--

  • 集成 Windows 身份验证:否
  • 允许匿名:关闭
  • Caler:同一站点页面上的链接 (GET)
  • 模拟:默认关闭(甚至不知道如何在 asp.net mvc 中启用它)

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    我在使用表单身份验证的网站上遇到了类似的问题,我能够通过使用提供的代码 here 作为线程中的第二个回复来解决这个问题。

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    // Add the current authentication cookie to the request
    HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
    Cookie authenticationCookie = new Cookie(
        FormsAuthentication.FormsCookieName,
        cookie.Value,
        cookie.Path,
        HttpContext.Current.Request.Url.Authority);
    
    req.CookieContainer = new CookieContainer();
    req.CookieContainer.Add(authenticationCookie);
    
    WebResponse res = req.GetResponse();
    

    【讨论】:

      【解决方案2】:

      我想你想要这个:

      req.Credentials = CredentialCache.DefaultCredentials;
      

      【讨论】:

      • 好吧,我尝试了 DefaultCredentials 和 DefaultNetworkCredentials。两者都无所谓
      • 您的网站是否在启用集成 Windows 身份验证并关闭允许匿名的情况下运行?什么在打电话?如果您的调用者本身就是一个网站,那么您是否打开了模拟并且正确配置了 Kerberos 委派?
      • 感谢您的评论。不确定模拟和 kerberos 委托。我该如何检查?把我知道的放在 OP 中。
      【解决方案3】:

      您将需要启用集成 Windows 身份验证。

      我不知道在 ASP.NET MVC 中会发生什么,但在 ASP.NET Web 窗体模拟中是通过以下方式打开的:

      <identity impersonate="true"> 
      

      在 web.config 中。

      【讨论】:

        猜你喜欢
        • 2011-04-01
        • 2016-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-26
        • 1970-01-01
        • 2016-07-07
        相关资源
        最近更新 更多