具有 ASP.NET 身份验证的控制台应用程序
情况如下,我有一个控制台应用程序,需要每天运行一次,并向需要身份验证才能查看的页面发出一些请求。这些页面托管在一个非常基本的 ASP.Net Web 应用程序中。
所以,我知道为了使请求成功通过,我必须向服务器进行身份验证。因此,我已将控制台应用程序连接到我用于 Web 应用程序的 ASP.Net 会员提供程序,并且它成功确定一组凭据是否有效。但是,在调用 Membership.ValidateUser() 后,我发出的任何请求都会显示登录屏幕。经过一些阅读后,这似乎是因为我缺少保留我的登录或您拥有的东西的重要 cookie 信息。
我使用基本的 WebClient 来发出请求,然后读取/丢弃结果。
所以问题的核心是:是否有一种简单的方法来验证登录信息并保留它,以便我可以成功发出请求,或者这是否与我发现的其他两个问题完全相同,需要WebClient 向login.aspx 页面发出“手动”登录请求并尝试从那里保留cookie?
我引用的问题是:
对 ASP.NET MVC 用户进行身份验证来自 WPF 应用程序
和
Here's the situation, I've got a console application that needs to run once a day and make a few requests to pages that require authentication to view. The pages are hosted in a really basic ASP.Net Web Application.
So, I know that in order for the requests to go through successfully I have to authenticate with the server. So I've hooked up the console application to the ASP.Net Membership Provider I'm using for the web app and it successfully determines if a set of a credentials are valid. However, after calling Membership.ValidateUser() any requests I make just get the login screen. After doing some reading it seems that this is because I'm missing the important cookie information that persists my login or what-have-you.
I'm using a basic WebClient to make the requests and then reading/discarding the result.
So the meat of the question is this: Is there a simple way to validate the login information and hold on to it so that I can make the requests successfully, or is this the exact same case as the other two questions I found that require the WebClient to make a "manual" login request to the login.aspx page and try to hold on to the cookie from there?
The questions I'm referencing are:
Authenticating ASP.NET MVC user from a WPF application
and
Login to website and use cookie to get source for another page
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 FormsAuthentication,网络服务器必须为您生成 Forms 身份验证票证。最好的(唯一的?)方法是登录该网站,所以我只需像其他问题一样登录即可。
With FormsAuthentication the webserver has to generate a Forms Authentication Ticket for you. The best (only?) way to do this is to log into the site, so I'd just log in like the other questions.
如果目的是将数据发送到服务器和/或从服务器获取数据,那么最合乎逻辑的架构可能是使用 ASMX 或 WCF 创建 Web 服务。然后将服务配置为使用安全令牌,例如用户名令牌或 SAML 令牌。当服务器代码更改其数据模型时,这将使客户端不太可能崩溃。
否则,如果您只想使用基本的 WebClient,则必须找到一种方法将您的凭据传递到登录页面并保留从登录请求返回的登录 cookie。然后,确保登录 cookie 包含在所有后续请求中,类似于您引用的 Stack Overflow 问题“登录网站并使用 cookie 获取另一个页面的源代码”。
If the intent is to send data to the server and/or get data from the server, then the most logical architecture is probably to create a web service using either ASMX or WCF. Then configure the service to use a security token, such as a username token or a SAML token. This will make the client less likely to break when the server code changes its data model.
Otherwise, if you wish to use only a basic WebClient, then you will have to find a way to pass your credentials to the login page and retain the login cookie that is returned from the login request. Then, make sure that the login cookie is included on all subsequent requets, similar to the Stack Overflow question that you referenced, "Login to website and use cookie to get source for another page".