// GET: UserProfile
        public ActionResult Index()
        {
            string clientId = ConfigurationManager.AppSettings["ida:ClientID"];
            string appKey = ConfigurationManager.AppSettings["ida:Password"];
            string graphResourceID = "https://graph.windows.net";
            string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
            string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
            string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

            bool validTokenPresent = true;
            TodoListWebApp.Models.TokenCacheEntry tce = null;
            //get a token using the cached values
            var existing = db.TokenCache.FirstOrDefault(a => (a.SignedInUser==signedInUserID) && (a.ResourceID == graphResourceID));
            if(existing!=null) //we have a token cache entry
            {
                tce = existing;
                //if the access token is expired
                if ( tce.Expiration.DateTime  < DateTime.Now)
                {
                    //use the refresh token to get a fresh set of tokens
                    try
                    {
                        ClientCredential clientcred = new ClientCredential(clientId, appKey);
                        AuthenticationContext authContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantID));
                        AuthenticationResult result = authContext.AcquireTokenByRefreshToken(tce.RefreshToken, clientId, clientcred, graphResourceID);
                        TodoListWebApp.Models.TokenCacheEntry tce2 = new TodoListWebApp.Models.TokenCacheEntry
                        {
                            SignedInUser = signedInUserID,
                            TokenRequestorUser = result.UserInfo.UserId,
                            ResourceID = graphResourceID,
                            AccessToken = result.AccessToken,
                            RefreshToken = result.RefreshToken,
                            Expiration = result.ExpiresOn.AddMinutes(-5)
                        };
                        db.TokenCache.Remove(tce);
                        db.TokenCache.Add(tce2);
                        db.SaveChanges();
                        tce = tce2;
                    }
                    catch
                    {
                        // the refresh token might be expired
                        tce = null;
                    }
                }
            } else // we don't have a cached token
            {
                tce = null;// it's already null, but for good measure...
            }

            if (tce != null)
            {
               // CallContext currentCallContext = new CallContext { AccessToken = tce.AccessToken, ClientRequestId = Guid.NewGuid(), TenantId = tenantID, ApiVersion = "2013-11-08" };

                 CallContext currentCallContext = new CallContext(tce.AccessToken, Guid.NewGuid(), "2013-11-08");

                GraphConnection graphConnection = new GraphConnection(currentCallContext);
                User user = graphConnection.Get<User>(userObjectID);
                return View(user);
            }
            else
            {
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return View();
            }
        }
Exemple #2
0
        // GET: UserProfile
        public ActionResult Index()
        {
            string clientId        = ConfigurationManager.AppSettings["ida:ClientID"];
            string appKey          = ConfigurationManager.AppSettings["ida:Password"];
            string graphResourceID = "https://graph.windows.net";
            string signedInUserID  = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
            string tenantID        = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
            string userObjectID    = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

            bool validTokenPresent = true;

            TodoListWebApp.Models.TokenCacheEntry tce = null;
            //get a token using the cached values
            var existing = db.TokenCache.FirstOrDefault(a => (a.SignedInUser == signedInUserID) && (a.ResourceID == graphResourceID));

            if (existing != null) //we have a token cache entry
            {
                tce = existing;
                //if the access token is expired
                if (tce.Expiration.DateTime < DateTime.Now)
                {
                    //use the refresh token to get a fresh set of tokens
                    try
                    {
                        ClientCredential      clientcred           = new ClientCredential(clientId, appKey);
                        AuthenticationContext authContext          = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantID));
                        AuthenticationResult  result               = authContext.AcquireTokenByRefreshToken(tce.RefreshToken, clientId, clientcred, graphResourceID);
                        TodoListWebApp.Models.TokenCacheEntry tce2 = new TodoListWebApp.Models.TokenCacheEntry
                        {
                            SignedInUser       = signedInUserID,
                            TokenRequestorUser = result.UserInfo.UserId,
                            ResourceID         = graphResourceID,
                            AccessToken        = result.AccessToken,
                            RefreshToken       = result.RefreshToken,
                            Expiration         = result.ExpiresOn.AddMinutes(-5)
                        };
                        db.TokenCache.Remove(tce);
                        db.TokenCache.Add(tce2);
                        db.SaveChanges();
                        tce = tce2;
                    }
                    catch
                    {
                        // the refresh token might be expired
                        tce = null;
                    }
                }
            }
            else            // we don't have a cached token
            {
                tce = null; // it's already null, but for good measure...
            }

            if (tce != null)
            {
                // CallContext currentCallContext = new CallContext { AccessToken = tce.AccessToken, ClientRequestId = Guid.NewGuid(), TenantId = tenantID, ApiVersion = "2013-11-08" };

                CallContext currentCallContext = new CallContext(tce.AccessToken, Guid.NewGuid(), "2013-11-08");

                GraphConnection graphConnection = new GraphConnection(currentCallContext);
                User            user            = graphConnection.Get <User>(userObjectID);
                return(View(user));
            }
            else
            {
                ViewBag.ErrorMessage = "AuthorizationRequired";
                return(View());
            }
        }