c# - Want to Create a Common WebAPI Login That Many Sites Will Use -
we need create simple site authentication/authorization. use latest oauth , owin , microsoft identity. assuming webapi way go new , mvc in general let me know if better way out there.
i have created simple login service web api has account
controller. here login
action use:
[allowanonymous] public actionresult login(string returnurl) { viewbag.returnurl = returnurl; return view(); }
and here simple client web app call login service app.
public async task<actionresult> index() { var client = new httpclient(); var response = await client.getasync("http://localhost:19604/account/login"); string thing = await response.content.readasstringasync(); return content(thing); }
this returns me view wanting email
, password
, remember me
, such. however, buttons not work. when click login
button 404 on /account/login
. suspect because webapi controller no longer hooked , client side not have account
controller.
is return content(thing);
causing problem , breaking it? need hard-code actions on login service go specific urls? or wanting not possible?
in end, want login service web api return me credentials/token logged in users claims if had logged in via client app itself.
first, webapi controllers
, don't return view. return ihttpactionresult
(put in more sensible way, return http codes)
the login
button preferably goes login action of account controller
in mvc context.
you cannot directly want unless in customised way.
for e.g.
- on clicking
login
button, redirect callprivate method
within controller itself. - call login method of webapi project inside private method.
- if login succeeds, (say if http 200 returned), redirect call corresponding
mvc controller
. if not, (say, on http 404 errors) gocustom error page
.
or else, can implement custom membership. decorate controller [custommembershipattribute] (here) attribute. see here
as side note: prefer using restsharp rather creating new instance of httpclient
, using it. code looks clumsy me ;)
Comments
Post a Comment