在本月底最終更新後獲取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/Login
POST
控制器甚至沒有被上面的代碼擊中。我究竟做錯了什麼?
編輯:兩個登錄的操作在我的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,但代碼是確定的。感謝您的時間。
可以添加控制器動作的簽名? (帳戶/登錄) – Erwin
'[使用AllowAnonymous] 公共的ActionResult登錄()'和'[HttpPost] [使用AllowAnonymous] 公共的ActionResult登錄(LoginModel模型,串RETURNURL)'用於button1的 – ojek
專家代碼是一個標準的C#應用程序,它是從我的其他asp MVC應用程序 – ojek