c# - Web API and MVC in the same project with Session States -


i'm been working around asp .net mvc application going take log in requests different sites different configurations (so cannot use formsauthentication sso way). way decided resolve creating temporal login request tokens, each token used once, , tokens, application make user session.

in order avoid generating tokens unnecesarily, thought of asking server first if user wasn't logged in already. , decided attempt via httpclient. code written follows.

            var client = new httpclient { baseaddress = new uri("http://mywidget.com") };             client.timeout = timespan.frommilliseconds(18000);             client.defaultrequestheaders.accept.clear();             client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));              var response = client.getasync(string.format("/userislogged/{0}", userid)).result;             response.ensuresuccessstatuscode();             bool islogged = response.content.readasasync<bool>().result;             return islogged; 

i came 2 possible ways make validation. wrote same action in web api , in mvc follows.

    [httpget]     public bool userislogged(int userid)     {         return (httpcontext.current.session != null && ((int)httpcontext.current.session["userid"]) == userid);     } 

with both found problems, , don't know server better purposes.

the mvc action returned answer html, , client couldn't handle answer, , haven't found yet way handle correctly.

the api action handles answer, web api doesn't handle session states default; must tweak can, , don't know if both sessions going same.

i'm not sure 1 of them right way go, or if should try alternative problem.

thank you.

the mvc actions should not return booleans. not mean need return whole view object, querying action result full html, hard parse.

you can force action methods return json or plain text. type of action still actionresult (it might optionally jsonresult), can return json() or content().

for example:

public actionresult userislogged(int userid)  {     if (....)     {         return json(true);     }     return json(false);  } 

i suggest web api actions return json aswell, instead of primitives. easier client parse response.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -