asp.net - Return user roles from bearer token of Web API -


i developing web api 2 project. authentication using bearer token. on successful authentication api returns json object.

{"access_token":"vn2kwvz...",    "token_type":"bearer",    "expires_in":1209599,    "username":"username",    ".issued":"sat, 07 jun 2014 10:43:05 gmt",    ".expires":"sat, 21 jun 2014 10:43:05 gmt"} 

now want return user roles in json object. changes need make in order user roles json response?

after searching lot found can create custom properties , can set them authentication ticket. in way can customize response can have custom values may required @ caller end.

here code send user roles along token. requirement. 1 can modify code send required data.

public override async task grantresourceownercredentials(oauthgrantresourceownercredentialscontext context)     {         using (usermanager<applicationuser> usermanager = _usermanagerfactory())         {             applicationuser user = await usermanager.findasync(context.username, context.password);              if (user == null)             {                 context.seterror("invalid_grant", "the user name or password incorrect.");                 return;             }              claimsidentity oauthidentity = await usermanager.createidentityasync(user,                 context.options.authenticationtype);              claimsidentity cookiesidentity = await usermanager.createidentityasync(user,                 cookieauthenticationdefaults.authenticationtype);             list<claim> roles = oauthidentity.claims.where(c => c.type == claimtypes.role).tolist();             authenticationproperties properties = createproperties(user.username, newtonsoft.json.jsonconvert.serializeobject(roles.select(x=>x.value)));              authenticationticket ticket = new authenticationticket(oauthidentity, properties);             context.validated(ticket);             context.request.context.authentication.signin(cookiesidentity);         }     }    public static authenticationproperties createproperties(string username, string roles)     {         idictionary<string, string> data = new dictionary<string, string>         {             { "username", username },             {"roles",roles}         };         return new authenticationproperties(data);     } 

this return me out put as

`{"access_token":"vn2kwvz...",  "token_type":"bearer",  "expires_in":1209599,  "username":"username",  ".issued":"sat, 07 jun 2014 10:43:05 gmt",  ".expires":"sat, 21 jun 2014 10:43:05 gmt"  "roles"=["role1","role2"] }` 

hope information helpful one. :)


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -