How do I pass my access token via javascript with IdentityServer3? -


i have asp.net mvc app lot of javascript calls. i've protected mvc action, redirected identity server, login, , redirected client. can make subsequent calls through mvc, how access token , use in ajax calls?

here's startup.cs file:

public void configuration(iappbuilder app) {     // tell microsoft not try map .net's claimstypes     jwtsecuritytokenhandler.inboundclaimtypemap = new dictionary<string, string>();      app.usecookieauthentication(new cookieauthenticationoptions     {         authenticationtype = "cookies"     });      const string svcurl = "https://localhost/svc.security";      app.useopenidconnectauthentication(new openidconnectauthenticationoptions     {         authority = svcurl,         clientid = "nedd_client",         redirecturi = "http://localhost:61207/",         responsetype = "code id_token token",          // ask 'roles' claims & access web services         scope = "openid profile",          signinasauthenticationtype = "cookies",          notifications = new openidconnectauthenticationnotifications         {             authorizationcodereceived = async n =>             {                 // filter "protocol" claims                 var claims = new list<claim>(from c in n.authenticationticket.identity.claims                                              c.type != "iss" &&                                                    c.type != "aud" &&                                                    c.type != "nbf" &&                                                    c.type != "exp" &&                                                    c.type != "iat" &&                                                    c.type != "nonce" &&                                                    c.type != "c_hash" &&                                                    c.type != "at_hash"                                              select c);                  // userinfo data                 var userinfoclient = new userinfoclient(new uri(svcurl + "/connect/userinfo"), n.protocolmessage.accesstoken);                  var userinfo = await userinfoclient.getasync();                 userinfo.claims.tolist().foreach(ui => claims.add(new claim(ui.item1, ui.item2)));                  // access token                 var tokenclient = new oauth2client(new uri(svcurl + "/connect/token"), "nedd_client", "secret");                  var response = await tokenclient.requestauthorizationcodeasync(n.code, n.redirecturi);                  claims.add(new claim("access_token", response.accesstoken));                 claims.add(new claim("expires_at", datetime.now.addseconds(response.expiresin).tolocaltime().tostring()));                 claims.add(new claim("id_token", n.protocolmessage.idtoken));                  n.authenticationticket = new authenticationticket(new claimsidentity(claims.distinct(new claimcomparer()), n.authenticationticket.identity.authenticationtype), n.authenticationticket.properties);             },         }      });  } 

and here's sample ajax call:

$.ajax({     type: 'get',     url: "https://localhost/svc.security/connect/userinfo",     //headers: { "authorization": "bearer " + my.getaccesstoken() },   // access token cookie?  }).done(function (data, textstatus, jqxhr) {     show(json.parse(jqxhr.response));        

i realized real problem fact that, after logging in, cookie asp.net server cookie , not accessible client.

i added method mvc controller javascript retrieve token server had.

    [authorize]     public actionresult getaccesstoken()     {         var token = (system.web.httpcontext.current.user.identity claimsidentity).findfirst("access_token");         if (token != null)         {             return content(token.value);         }          return content("");     } 

then javascript this, calling token before making ajax call web service.

function getaccesstoken() {     var returnval = "";      var xhr = new xmlhttprequest();     xhr.open("get", "http://localhost:61207/home/getaccesstoken", false);     xhr.send();     returnval = xhr.responsetext;      return returnval; } 

there may different solution more elegant, once had clear in mind available @ client , server, seemed best solution.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -