2011-10-11 168 views
0

我们正在使用表单身份验证构建需要来自IIS服务器的一些数据的命令行应用程序。如何从命令行应用程序执行表单身份验证?我试过了,但发生的一切就是请求被重定向到登录页面。命令行应用程序,IIS需要表单身份验证

我猜如果我可以包含一个身份验证cookie请求与正确的凭据,下载会很好。

我可以控制客户端和服务器。我可以在web.config中设置machineKey,但无法弄清楚如何在命令行应用程序中设置它。这必须是相同的验证密钥才能以正确的格式加密cookie?

该服务器是用asp-net mvc编写的。

var request = (HttpWebRequest)WebRequest.Create(uri); 
var formsCookiePath = "/"; 
var cookieName = ".FormName"; 
var domain = "localhost"; 
var username = "username"; 
var password = "pa$$word"; 

var ticket = new FormsAuthenticationTicket(1, 
    username, 
    DateTime.Now, 
    DateTime.Today.AddYears(10), 
    true, 
    password, 
    formsCookiePath); 

var encryptedTicket = FormsAuthentication.Encrypt(ticket); 

var authenticationCookie = new Cookie(
    cookieName, 
    encryptedTicket, 
    formsCookiePath, 
    domain) 
    { 
    HttpOnly = true, 
    Secure = false 
    }; 

request.CookieContainer = new CookieContainer(); 
request.CookieContainer.Add(authenticationCookie); 

request.UserAgent = "Internal downloader"; 

var loWebResponse = (HttpWebResponse)request.GetResponse(); 

更新: 基于从Zruty我用这个例子来生成身份验证Cookie的答案:

ASP.NET Authorization from Console Application & Timeout

回答

0

要在本地模拟验证码你的客户吗?我认为你走错了路。

您应该让服务器工作(cookie生成)到服务器。随后发出两个请求:一个包含您的登录信息(服务器将生成一个cookie),另一个包含提供的cookie。

相关问题