ASP.NET授权从控制台应用程序和放大器;暂停 [英] ASP.NET Authorization from Console Application & Timeout

查看:110
本文介绍了ASP.NET授权从控制台应用程序和放大器;暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关什么它的价值,我得到了一个控制台应用程序本身与现有的ASP.NET Web应用程序进行身份验证。它只是通过POST请求包含视图状态信息(就像一个浏览器已经提出的要求)登录页面模仿登录页面。

For what its worth, I managed to get a console application to authenticate itself with an existing ASP.NET web application. It just mimics the login page by making a post request to the login page containing the viewstate information (just as if a browser had made the request).

然后,它捕捉在一个响应的CookieContainer发回的饼干。

It then catches the cookie sent back in response in a CookieContainer.

应用程序然后继续进行多次请求到同一个页面(这需要授权)。页面做了一些工作,并写出一个文件的服务器上。

The application then goes on to make multiple requests to the same page (which requires authorization). The page does some work and writes out a file on the server.

我遇到的问题是,假登录后,前两个请求都通过好。第三个要求从来没有收到响应。下面是code我使用做出要求:

The problem I'm encountering is that after the phony login, the first two requests go through okay. The third request never even receives a response. Below is the code I'm using to make the requests:

要登录:

private static CookieContainer PerformLoginRequest()
{
    string loginURL = "http://www.myDomain.com/mySite/login.aspx";

    //Set up the request and give it a cookie container.
    CookieContainer cookieJar = new CookieContainer();
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(loginURL);
    request.CookieContainer = cookieJar;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    //Grab the request body so we can put important information in it.
    Stream requestBody = request.GetRequestStream();
    string postData = "__VIEWSTATE=GIBBERISH&UserName=USERNAME&Password=PASSWORD%21&LoginButton.x=0&LoginButton.y=0";

    //Throw the string into the body.
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] byte1 = encoding.GetBytes(postData);
    requestBody.Write(byte1, 0, byte1.Length);
    requestBody.Close();

    //Get the response to the login request.
    WebResponse response = request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string result = reader.ReadToEnd();
    reader.Close();

    return cookieJar;
}

要请求一个页面:

private static long RequestPage(string url)
{
    //If we've already got a CookieContainer, then we've logged in already. Otherwise, log in.
    if (cookieJar == null)
    {
        try
        {
            cookieJar = PerformLoginRequest();
        }
        catch
        {
            throw;
        }
    }

    //Set up the request.
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.CookieContainer = cookieJar;
    request.Method = "GET";
    request.ContentType = "application/x-www-form-urlencoded";
    request.Timeout = timeout; //5 minutes.

    WebResponse response = request.GetResponse();

    return response.ContentLength;
}

该cookieJar和超时变量是全球应用程序。

The cookieJar and timeout variables are global to the application.

任何想法什么会导致此?

Any idea what would cause this?

更新(2010-01-17,3:30 PM EST)

当我使用Fiddler运行启动应用程序,一切顺利经过精细。所有的请求的功能,就像我期望的那样。

When I launch the application with Fiddler running, everything goes through fine. All the requests function, just as I would expect.

我检查IIS日志文件。在他们身上我能看到intial登录请求,登录后重定向,并为前两页的请求。但在那之后什么都没有。这就像要求甚至不使它的Web服务器。

I inspected the IIS log files. In them I can see the intial login request, the redirect after login, and the requests for the first two pages. But after that...nothing. It's like the requests don't even make it to the web server.

更新(2010-01-18 11:29 16:00)

我更新了code往上顶,以反映由克里斯·贝伦斯B.建议的修改。但无济于事。在这重要的情况下,调用上述功能的循环是这样的:

I updated the code up top to reflect the changes suggested by Chris B. Behrens. But to no avail. In case it matters, the loop that calls the above functions looks like this:

private static void RegenerateNecessaryForms(MyEntities context)
{
    var items = context.Items.Where(i => i.NeedsProcessing == true);

    var area = context.Areas;
    foreach (Item i in items)
    {
        bool success = true;
        foreach (Area area in areas)
        {
            try
            {
                string url = string.Format("http://{0}/mySite/process.aspx?item={1}&area={2}", targetDomain, i.ItemName, area.AreaName);
                long result = RequestPage(url, m);
            }
            catch
            {
                success = false;
            }
        }

        if (success)
        {
            i.NeedsProcessing = false;
        }
    }
}

更新(2010-01-18,13:03 EST)
这可能是最有用的信息我已经能够到目前为止提供的。

Update (2010-01-18, 1:03 PM EST) This is probably the most useful information I've been able to provide so far.

我又看IIS日志。当发送的登录请求,我可以刷新记录并立即显示出来。但是,没有一个页面请求的显示,直到我关闭控制台应用程序。这里是日志的相关位:

I was looking at the IIS logs again. When the login request is sent, I can flush the logs and it shows up immediately. However, none of the page requests show up until I close the console application. Here's the relevant bit of the log:

2011-01-18 17:44:54 X.X.X.X POST /MySite/login.aspx - 80 - X.X.X.X - 302 0 0 148
2011-01-18 17:44:54 X.X.X.X GET /MySite/default.aspx - 80 AutomatedUser X.X.X.X - 200 0 0 48
2011-01-18 17:54:33 X.X.X.X GET /MySite/process.aspx model=ITEM_ONE&area=US 80 AutomatedUser X.X.X.X - 200 0 995 579100
2011-01-18 17:54:33 X.X.X.X GET /MySite/process.aspx model=ITEM_ONE&area=CANADA 80 AutomatedUser X.X.X.X - 200 0 995 553247
2011-01-18 17:54:33 X.X.X.X GET /MySite/process.aspx model=ITEM_TWO&area=US 80 AutomatedUser X.X.X.X - 200 0 995 532824

每个在GET请求到process.aspx对应于上述RequestPage方法的调用。行request.GetResponse();只需要大约10秒左右,但它看起来像某种方式的要求永远不会(至少根据IIS),直到我关闭应用程序完成。 (如果你看一下每行的最后一个数字,那就是花费的时间来服务以毫秒为单位的页面数量,但我​​得到的回应回到了应用程序在更短的时间比)。所以这是可能的(甚至可能是可能的)IIS正在限制连接数将接受来自一个IP。

Each of the GET requests to process.aspx corresponds to a call to the RequestPage method above. The line "request.GetResponse();" only takes about 10 seconds or so, but it looks like somehow that request never gets completed (at least according to IIS) until I close the application. (If you look at the last number on each line, that's the amount of time it took to serve the page in milliseconds, but I got the response back in the application in much less time than that.) So it's possible (probably even likely) that IIS is restricting the amount of connections it will accept from one IP.

这使我想到了一个问题:这是什么?关于我做,使它们保持开放这样的要求

Which brings me to the question: What is it about the requests that I'm making that causes them to stay "open" like that?

该请求不返回一个HTML页面(如果该事项),他们返回一个PDF。

The requests don't return an HTML page (if that matters) they return a PDF.

推荐答案

你没有明确关闭您的要求:

You're not explicitly closing your request:

//Get the response to the login request.
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string result = reader.ReadToEnd();

reader.Close();

return cookieJar;

请参阅如果这样做的伎俩。

See if that does the trick.

这篇关于ASP.NET授权从控制台应用程序和放大器;暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆