c# - JSON, getting information using HttpPost -
i'm recreating old cordova app in xamarin forms pcl , need access method that's on server, providing username , password , storing information comes back:
[httpget] public jsonresult loginuser(string username, string password) { bool responseresult = false; iebuser user = null; string errmsg = string.empty; try { if (string.isnullorempty(username)) { throw new exception("username empty"); } else if (string.isnullorempty(password)) { throw new exception("username password"); } // connect db , find user if can user = selfservicemembership.getuserbyusername(username); // if no suer user wasn't found or db errored if (user == null) { throw new exception("username not found"); } // decrypt pw , see if match username's account passwordhash ph = new passwordhash(); password = ph.generatehash(password); if (user.hashword != password) { throw new exception("password not match 1 on our records"); } responseresult = true; } catch (exception ex) { errmsg = ex.message; } if (responseresult) { return json(new { result = responseresult, user = new { userid = user.userid, username = user.username, firstname = user.firstname, lastnmae = user.lastname, email = user.email } }, jsonrequestbehavior.allowget); } return json(new { result = responseresult, errormessage = errmsg }, jsonrequestbehavior.allowget); }
the old javascript code called method looks this:
// gets user info web service loginuser: function (username, password){ responsedata = null; $.ajax({ type: "get", url : app.currentcompanydetails.webserviceurl + this.apiroot + this.loginusercall, datatype: "json", data: { username: username, password: password }, async: false, crossdomain: true, success: function(response) { responsedata = response; }, error: function (xhr, status, msg) { alert(msg); } }); return responsedata; },
i can't seem find definitive answer anywhere on how accomplish in c#
here example showing how make post call. guess if change httpmethod.get
should work too.
my example sends username
, password
rest api , if ok, returns cookie
response. can change adapt needs.
private static cookie configuraracessoged() { uri uri = new uri("url_from_api"); var req = new httprequestmessage(httpmethod.post, uri); var reqbody = @"{ 'username':'username', 'password':'password' }"; req.content = new stringcontent(reqbody, encoding.utf8, "application/json"); cookiecontainer cookies = new cookiecontainer(); httpclienthandler handler = new httpclienthandler(); handler.cookiecontainer = cookies; httpclient client = new httpclient(handler); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/xml")); httpresponsemessage resp = null; try { resp = client.sendasync(req).result; } catch (exception ex) { throw new exception("something went wrong"); } if (!resp.issuccessstatuscode) { string message; if (resp.statuscode == httpstatuscode.unauthorized || resp.statuscode == httpstatuscode.forbidden || resp.statuscode == httpstatuscode.redirect) message = "permission denied."; else message = resp.reasonphrase; throw new exception(message); } ienumerable<cookie> responsecookies = cookies.getcookies(uri).cast<cookie>(); if (responsecookies.firstordefault() != null) return responsecookies.firstordefault(); return null; }
here method api:
[httppost, route("login")] public iactionresult login([fromservices] userservice userservice, [frombody]loginmodel login) { using (var session = sessionfactory.opensession()) using (var transaction = session.begintransaction()) { user user = userservice.login(session, login.username, login.password); if (user == null) return badrequest("invalid user"); var identity = new claimsidentity("filesystem"); identity.addclaim(new claim(claimtypes.name, login.username)); foreach (role r in user.roles) { identity.addclaim(new claim(claimtypes.role, r.nome)); } var principal = new claimsprincipal(identity); httpcontext.authentication.signinasync("filesystem", principal).wait(); } return ok(); }
Comments
Post a Comment