model view controller - mvc azure ad token expiration -


i'm building mvc5 app hosted on azure in term used throught wpf app.

as need check user group membership implemented graph api following guidance in article : https://azure.microsoft.com/fr-fr/documentation/samples/active-directory-dotnet-graphapi-web/

it works quite fine time after user logged in access following controller raise access denied error :

public async task<actionresult> index()     {          string uid = claimsprincipal.current.findfirst("http://schemas.microsoft.com/identity/claims/objectidentifier").value;         activedirectoryclient client = authenticationhelper.getactivedirectoryclient();         iuser aduser = client.users.where(u => u.objectid == uid).executeasync().result.currentpage.singleordefault();          ilist<group> groupmembership = new list<group>();          var userfetcher = (iuserfetcher)aduser;          ipagedcollection<idirectoryobject> pagedcollection = await userfetcher.memberof.executeasync();                 {             list<idirectoryobject> directoryobjects = pagedcollection.currentpage.tolist();             foreach (idirectoryobject directoryobject in directoryobjects)             {                 if (directoryobject group)                 {                     var group = directoryobject group;                     groupmembership.add(group);                 }             }             pagedcollection = await pagedcollection.getnextpageasync();         } while (pagedcollection != null);          viewbag.user = aduser.userprincipalname;         viewbag.userdn = aduser.displayname;         viewbag.usergn = aduser.givenname;         viewbag.usermail = aduser.mail;         viewbag.usersn = aduser.surname;          return view(groupmembership);     } 

the exception raised on getactivedirectoryclient(), code of method strict copy/paste article in link , looks :

internal class authenticationhelper {     public static string token;      /// <summary>     ///     async task acquire token application.     /// </summary>     /// <returns>async token application.</returns>     public static async task<string> acquiretokenasync()     {         if (token == null || token.isempty())         {             throw new exception("authorization required. ");         }         return token;     }      /// <summary>     ///     active directory client application.     /// </summary>     /// <returns>activedirectoryclient application.</returns>     public static activedirectoryclient getactivedirectoryclient()     {         uri baseserviceuri = new uri(constants.resourceurl);         activedirectoryclient activedirectoryclient =             new activedirectoryclient(new uri(baseserviceuri, constants.tenantid), async () => await acquiretokenasync());         return activedirectoryclient;      } } 

this code works right after user has logged in after times token become null , exception raised.

i'm guessing related expiration time, there's way set auto refresh on token ?

thanks !

thanks answering, don't have yet set [authorize] tag azure ad group membership grant access controllers , haven't yet figured out how achieve :)

it seems appliying mofifications authenticationhelper solved issue :

public static activedirectoryclient getactivedirectoryclient()     {         uri baseserviceuri = new uri(constants.resourceurl);         string userobjectid = claimsprincipal.current.findfirst("http://schemas.microsoft.com/identity/claims/objectidentifier").value;         authenticationcontext authcontext = new authenticationcontext(authority, new naivesessioncache(userobjectid));         clientcredential credential = new clientcredential(clientid, appkey);          activedirectoryclient activedirectoryclient = new activedirectoryclient(new uri(baseserviceuri, constants.tenantid), async () =>         {             var result = await authcontext.acquiretokensilentasync(graphurl, credential, new useridentifier(userobjectid, useridentifiertype.uniqueid));              return result.accesstoken;         });          return activedirectoryclient;      } 

i don't know if that's clean way thing @ least works.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -