请求未获取凭据的基本身份验证

时间:2015-02-27 16:04:27

标签: c# authentication httpwebrequest basic-authentication

首先我要说的是我没有设计这个认证系统。我不是粉丝。

它的工作方式取决于是否可以加载单独服务器上的图像。所以让我打破身份验证流程。

  
      
  1. 我想去server.com
  2.   
  3. 如果未登录,请将我重定向至https://authserver.com
  4.   
  5. https://authserver.com的代码加载图片,如果无法加载图片,则会提示基本身份验证窗口加载图片
  6.   
  7. 每当图片加载时,它都会重定向回server.com
  8.   

我正在尝试自动访问server.com上托管的XML Feed。我现在使用的代码是:

  CredentialCache mCache = new CredentialCache();
    mCache.Add(new Uri("https://authserver.com"), "Basic", new NetworkCredential(mUsername, mPassword));

    string mUrl = "http://server.com";

    var req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(mUrl));
    req.AllowAutoRedirect = true;
    req.PreAuthenticate = true;
    req.KeepAlive = true;
    req.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
    req.Credentials = mCache;

    WebResponse mResponse = req.GetResponse();

    string strResponse = new StreamReader(mResponse.GetResponseStream()).ReadToEnd();

我收到的唯一输出是身份验证页面的源代码。这是我所指的代码:

    <body>
          <script language="Javascript">
                var u = "https://authserver.com/login&redirect=http://server.com"

                function loaded () {
                      self.setTimeout('location.href=u' , 0);
                }

                function noLoad () {
                      theUrl = theUrl.replace(/^https/, "http")
                      self.setTimeout('location.href=u' , 0);
                }                       
          </script>         

          <p>
          <img src="https://authserver.com/image.gif"
                onLoad="loaded()" onError="noload()" onAbort="noload()" />

    </body>

所以我似乎只需要成功获取https://authserver.com/login的基本授权标头,然后它会将我重定向到服务器,但我使用的C#代码不起作用。有谁知道我做错了什么?

0 个答案:

没有答案