2012-10-09 26 views
0

在本月底最終更新後獲取auth cookie的ASP MVC + HttpWebRequest應用程序,我如何強制它工作?

我就可以了整整一天的工作,我不能讓它工作:

private void button1_Click(object sender, EventArgs e) 
{ 
    var loginUri = new Uri(@"http://localhost:5898/Account/Login"); 
    const string strLoginData = "Login=ojejq&Password=ojejqjejq&returnUrl=%2F"; 

    var cookie = GetAuthCookie(loginUri, strLoginData); 
} 

public CookieContainer GetAuthCookie(Uri loginUri, string data) 
{ 
    var cookieJar = new CookieContainer(); 
    var request = (HttpWebRequest)WebRequest.Create(loginUri); 
    request.Method = "POST"; 
    request.CookieContainer = cookieJar; 
    request.ContentLength = data.Length; 
    request.ContentType = "application/x-www-form-urlencoded"; 

    var myWriter = new StreamWriter(request.GetRequestStream()); 
    myWriter.Write(data); 
    myWriter.Close(); 
    request.GetResponse(); 

    return cookieJar; 
} 

在我ASP MVC的應用程序,我有一個/Account/LoginPOST控制器甚至沒有被上面的代碼擊中。我究竟做錯了什麼?

編輯:兩個登錄的操作在我的ASP MVC應用程序:

[AllowAnonymous] 
public ActionResult Login() 
{...} 

[HttpPost] 
[AllowAnonymous] 
public ActionResult Login(LoginModel model, string returnUrl) 
{...} 

二更新:加入登錄模型

public class LoginModel 
{ 
    [Required] 
    [Display(Name = "Login", ResourceType = typeof(NameResources))] 
    [StringLength(16, ErrorMessageResourceName = "LoginLengthError", ErrorMessageResourceType = typeof(NameResources), MinimumLength = 4)] 
    public string Login { get; set; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password", ResourceType = typeof(NameResources))] 
    [StringLength(32, ErrorMessageResourceName = "PasswordLengthError", ErrorMessageResourceType = typeof(NameResources), MinimumLength = 8)] 
    public string Password { get; set; } 
} 

第三次更新:我忘了提,在我的web.config我有cookieless =「AutoDetect」選項設置。我不知道它是否做出任何改變?

最終更新:首先,感謝您的時間,每個人都試圖幫助我得到upvote。我發現問題不在代碼中,而是在我的Visual Studio開發服務器中。它不知何故將我的按鈕1 POST請求重定向,在此過程中丟失了數據,並將該請求更改爲GET請求。我知道,werid,但代碼是確定的。感謝您的時間。

+1

可以添加控制器動作的簽名? (帳戶/登錄) – Erwin

+0

'[使用AllowAnonymous] 公共的ActionResult登錄()'和'[HttpPost] [使用AllowAnonymous] 公共的ActionResult登錄(LoginModel模型,串RETURNURL)'用於button1的 – ojek

+0

專家代碼是一個標準的C#應用​​程序,它是從我的其他asp MVC應用程序 – ojek

回答

5

我想你應該嘗試「用戶名」 insted的在「登錄」的:

const string strLoginData = "UserName=ojejq&Password=ojejqjejq&returnUrl=%2F"; 

,並嘗試這也:

public CookieCollection GetAuthCookie(Uri loginUri, string data) 
{ 
    var cookieJar = new CookieContainer(); 
    var request = (HttpWebRequest)WebRequest.Create(loginUri); 
    request.Method = "POST"; 
    request.CookieContainer = cookieJar; 
    request.ContentLength = data.Length; 
    request.ContentType = "application/x-www-form-urlencoded"; 

    var myWriter = new StreamWriter(request.GetRequestStream()); 
    myWriter.Write(data); 
    myWriter.Close(); 

    var webResponse = request.GetResponse() as HttpWebResponse; 
    return webResponse.Cookies; 
} 
+0

嗯,這應該打他的控制器方法(假設他的LogInModel有一個名爲UserName的屬性),但它不會使完整的代碼工作。 –

+0

Actully它適用於我,它返回了一個「__RequestVerificationToken」 –

+0

的cookie,我對第一個答案發表了評論(沒有GetAuthCookie()片段)。我認爲現在它應該工作,你有我的+1 –

相關問題