【问题标题】:ASP.NET authentication from a Windows Forms application来自 Windows 窗体应用程序的 ASP.NET 身份验证
【发布时间】:2013-02-19 14:31:06
【问题描述】:

我在 google 上搜索了“来自 Windows 窗体应用程序的 ASP.NET 身份验证”,但结果是“使用 ASP.NET 的 Windows 身份验证”,这不是我想要的。 p>

场景:

我有一个 ASP.NET 应用程序和一组 WCF 服务,它们通过 AJAX 调用向应用程序提供数据。这些 WCF 服务需要 ASP.NET 身份验证,这在浏览器中很好,因为有一个 cookie 提供身份验证信息,或要求用户通过登录页面进行身份验证。

我需要从 Windows 窗体应用程序调用这些服务,而不改变它们的工作方式。即 Windows 窗体应用程序将从服务接收 JSON 数据并对其进行处理。

问题:

我需要通过身份验证才能使用WCF服务,但由于这不是Web应用程序,没有cookie,无法显示登录页面!

问题:

如何从 Windows 窗体应用程序向 ASP.NET Web 应用程序提供身份验证?

【问题讨论】:

    标签: c# asp.net .net wcf authentication


    【解决方案1】:

    您需要为每个请求(如浏览器)存储 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];
                }
            }
        }
    

    【讨论】:

    • 很好的例子。唯一缺少的是对您的代码在 cookieContainer 中“修复”的错误的引用 - 以便我们知道您实际解决的问题以及错误是否仍然存在。
    • @beterthanlife 谢谢。这是我得到这个解决方法的地方:stackoverflow.com/questions/1047669/cookiecontainer-bug
    猜你喜欢
    • 2012-11-01
    • 2013-04-09
    • 2017-08-13
    • 2023-04-10
    • 2021-01-10
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2022-08-08
    相关资源
    最近更新 更多