2013-03-05 52 views
4

我在谷歌上搜索了「ASP.NET窗口應用程序的認證」,但結果是「使用ASP.NET的Windows身份驗證」,這不是我正在尋找的對於。從Windows窗體應用程序的ASP.NET身份驗證

場景:

我有一個ASP.NET應用程序和一組WCF服務的哪些應用程序提供的數據通過AJAX調用。這些WCF服務需要ASP.NET身份驗證,這對瀏覽器來說很好,因爲有一個cookie提供身份驗證信息,或者要求用戶通過登錄頁面進行身份驗證。

我需要從Windows窗體應用程序調用這些服務,而不改變它們的工作方式。即Windows窗體應用程序將從服務接收JSON數據並對其進行處理。

問題:

我需要進行身份驗證之前,我可以使用WCF服務,但因爲這不是一個Web應用程序,沒有任何的cookie,而不能顯示在登錄頁面!

問:

我如何從Windows提供認證窗體應用程序的ASP.NET Web應用程序?

回答

5

您需要爲瀏覽器等每個請求存儲cookie。一旦您發送登錄表單請求,Cookie變量就會爲您的下一個請求進行身份驗證。

static class Requester 
{ 
    static CookieContainer cookieJar = new CookieContainer(); 
    static string userAgent = ""; //specify your user agent 

    /// <summary> 
    /// Static method to request a web page. 
    /// It acts like a browser which means that keeps all cookies for depending domain. 
    /// </summary> 
    /// <param name="URL"></param> 
    /// <returns></returns> 
    static public FinalResponse sendRequest(string URL) 
    { 
     return sendRequest(URL, ""); 
    } 


    static public FinalResponse sendRequest(string URL, string parameters) 
    { 
     FinalResponse result = new FinalResponse(); 
     Stopwatch timer = new Stopwatch(); 
     HttpWebRequest request; 

     try 
     { 
      request = (HttpWebRequest)HttpWebRequest.Create(URL); 
      request.Referer = "http://google.com"; //specify your referer 
      request.CookieContainer = cookieJar; 
      request.UserAgent = userAgent; 
      BugFix_CookieDomain(); 
      request.AllowAutoRedirect = true; 
      if (parameters != "") 
      { 
       request.Method = "POST"; 
       request.ContentType = "application/x-www-form-urlencoded"; 
       request.ContentLength = parameters.Length; 

       //push parameters to request stream 
       StreamWriter myWriter = new StreamWriter(request.GetRequestStream()); 
       myWriter.Write(parameters); 
       myWriter.Close(); 
      } 
      //send request 
      result.requestTime = DateTime.Now; 
      timer.Start(); 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      timer.Stop(); 
      result.responseMilliseconds = timer.ElapsedMilliseconds; 
      BugFix_CookieDomain(); 
      using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
      { 
       result.document = sr.ReadToEnd(); 
       sr.Close(); 
       result.isSucceded = true; 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
      result.document = ""; 
      result.isSucceded = false; 
     } 
     return result; 
    } 


    /// <summary> 
    /// Call this function before all usage of cookieJar. 
    /// It fixes the bug (!) of CookieContainer Class. 
    /// </summary> 
    private static void BugFix_CookieDomain() 
    { 
     System.Type _ContainerType = typeof(CookieContainer); 
     Hashtable table = (Hashtable)_ContainerType.InvokeMember("m_domainTable", 
            System.Reflection.BindingFlags.NonPublic | 
            System.Reflection.BindingFlags.GetField | 
            System.Reflection.BindingFlags.Instance, 
            null, 
            cookieJar, 
            new object[] { }); 
     ArrayList keys = new ArrayList(table.Keys); 
     foreach (string keyObj in keys) 
     { 
      string key = (keyObj as string); 
      if (key[0] == '.') 
      { 
       string newKey = key.Remove(0, 1); 
       table[newKey] = table[keyObj]; 
      } 
     } 
    } 
+0

不錯的例子。只有缺少的東西是您的代碼在cookieContainer中「修復」的錯誤的引用 - 只是讓我們知道您實際正在處理的內容以及錯誤是否仍然存在。 – beterthanlife 2014-12-15 13:09:53

+0

@beterthanlife謝謝。這裏是我得到這個解決方法:http://stackoverflow.com/questions/1047669/cookiecontainer-bug – 2014-12-19 01:09:23

相關問題