Final update at the end of this post
I am working on it whole day, and i cannot make it work:
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;
}
In my ASP MVC application, I have a /Account/Login POST controller that is not even hit by above code. What am I doing wrong?
Edit: Two login actions in my asp mvc app:
[AllowAnonymous]
public ActionResult Login()
{...}
and
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginModel model, string returnUrl)
{...}
Second update: added login model
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; }
}
Third update: I forgot to mention, that in my web.config i have cookieless=”AutoDetect” option set. I don’t know if it makes any changes?
Final update: First of all, thank you guys for your time, everyone who tried to help me gets an upvote. I found out that the problem was not in the code, but in my visual studio dev server. It somehow redirected my button1 POST request, lost data in the process, and changed that request into a GET request. I know, werid, but the code was OK. Thank you for your time.
I think you should try “UserName” insted of “Login” in:
and try this too: