asp.net - OWIN WebApi Entity Framework with OAuth Identity -


i'm experimenting self hosted owin webapi/entity framework project

i've created startup class , configured both owin , webapi using useoauthbearerauthentication , useoauthauthorizationserver provider defined class deriving oauthauthorizationserverprovider

 provider = new applicationoauthserverprovider() // :oauthauthorizationserverprovider 

this class overrides

 public override async task grantresourceownercredentials(oauthgrantresourceownercredentialscontext context)     {} 

validate user creates claimsidentity returning token encoding associated claims in case nameidentifier, name , role (role "admin")

everything works expected , token returned. i'd take advantage of associated claims inside apicontroller. problem user.identityobject has authentiationtype isauthenticated , name properties associated claims not there , can't name property. see using

[authorize (roles="admin")] 

i'm able access apicontroller role claim available somewhere other claims i'm not able access;

is there way solve issue???

 [authorize (roles="admin")] public class testcontroller : apicontroller {      public async task<account> get()     {         var principal = user.identity;         .... find , return data user id     }  } 

here classes i've used

    public class startup     {     // method required.     public void configuration(iappbuilder app)     {         // use cors on server level         app.usecors(microsoft.owin.cors.corsoptions.allowall);          // configure owin authenticate incoming requests.         configureauth(app);         // use extension method provided webapi.owin library.         app.usewebapi(configurewebapi());     }      private void configureauth(iappbuilder app)     {         // make sure single instance of ef context created per owincontext.         app.createperowincontext<applicationdbcontext>(applicationdbcontext.create);          var oauthoptions = new oauthauthorizationserveroptions{             tokenendpointpath = new pathstring("/token"),             provider = new applicationoauthserverprovider(),              accesstokenexpiretimespan = timespan.fromdays(14),             // debug             allowinsecurehttp = true         };          // server added options object, specifies other configuration items,          // , passed middleware pipeline.         app.useoauthauthorizationserver(oauthoptions);          // indicate want return bearer tokens          // passing default implementation oauthbearerauthenticationoptions,         app.useoauthbearerauthentication(new oauthbearerauthenticationoptions());     }      private httpconfiguration configurewebapi()     {         var config = new httpconfiguration();          //add json formetters          // configure api routes         config.routes.maphttproute(             "defaultapi",             "api/{controller}/{id}",             new { id = routeparameter.optional });           return config;     } } 

applicationoauthserverprovider class

public class applicationoauthserverprovider : oauthauthorizationserverprovider {     public override async task validateclientauthentication(oauthvalidateclientauthenticationcontext context)     {         // call required...         await task.fromresult(context.validated());     }      public override async task grantresourceownercredentials(oauthgrantresourceownercredentialscontext context)     {           if (context.password == "password")         {             // create or retrieve claimsidentity represent              // claimsidentity created represent user data, including claims user should have.              claimsidentity identity = new claimsidentity(context.options.authenticationtype);             identity.addclaim(new claim(claimtypes.nameidentifier, "120"));             identity.addclaim(new claim(claimtypes.name, context.username));             identity.addclaim(new claim(claimtypes.role, "admin"));              // claimsidentity encoded access token             context.validated(identity);           }         else         {             context.seterror("invalid_grant", "the user name or password incorrect.");             context.rejected();         }      } } 


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -