asp.net - acessing username in web api controller when database table stores user as integer -
inside web api controllers, access user indicated in answer: https://stackoverflow.com/a/12705062/538962
sample code answer...
[authorize] public list<product> getproductsfromid() { string username = user.identity.name; return _productservice.getproductsfromusername(username); } the asp_net membership tables in scenario on different database server database server application runs on. database application has own users table identity column primary key on users table, , other tables include createdbyuserid , updatedbyuserid columns integers based off identity column in users table.
the issue if crud type operations depend on user being updated in tables integer, accessing username alone not sufficient; still have username's corresponding userid.
this done join users table, seems bit kludgy. best way go handling issue?
from perspective of asp.net web api, using membership provider , out-of-box formsauthentication kludgy, why not join? :) anyways, assuming web api consumed web clients , fa cool, can use userdata of fa ticket put in user id. way, don't need id hitting db every time. need create ticket , not let default out-of-box implementation you. then, in postauthenticaterequest event, can read user id ticket , set in identity. of course, need create own custom identity id property or if on .net 4.5, can use formsidentity can use nameidentifier claim store id, perhaps. check out - asp.net mvc - set custom iidentity or iprincipal.
Comments
Post a Comment