How to login to ASP.Net MVC website (Forms authentication) using HttpClient in Windows Store App? -
first created httpclienthandler cookie container
cookiecontainer cookies = new cookiecontainer(); httpclienthandler handler = new httpclienthandler(); handler.cookiecontainer = cookies; handler.usecookies = true; var hc = new httpclient(handler);
then hit base url cookie "__requestverificationtoken"
string r = await hc.getstringasync(baseurl);
then post username/password login url
httpcontent content = new formurlencodedcontent(new[] { new keyvaluepair<string, string>("username", "admin"), new keyvaluepair<string, string>("password", password), }); httpresponsemessage response = await hc.postasync(loginurl, content);
then server error "the required anti-forgery form field "__requestverificationtoken" not present".
but when check request in fiddler, can see "__requestverificationtoken" added in cookies of request.
then tried login manually in ie, , check kind of request ie sent.
then discovered ie put "__requestverificationtoken" in form, added cookie in form
new keyvaluepair<string, string>("__requestverificationtoken", cookies.getcookies(new uri(baseurl)).cast<cookie>().firstordefault(x => x.name == "__requestverificationtoken").value)
then got error
"validation of provided anti-forgery token failed. cookie "_requestverificationtoken" , form field "_requestverificationtoken" swapped."
then couldn't search result on google error.
any idea?
thanks
found issue.
the "__requestverificationtoken" added cookie different 1 added form.
so took value of "__requestverificationtoken" in form returned html string, worked!
Comments
Post a Comment