AcquireTokenOnBehalfOfAsync() public method

AcquireTokenOnBehalfOfAsync
public AcquireTokenOnBehalfOfAsync ( string scope, UserAssertion userAssertion ) : Task
scope string
userAssertion UserAssertion
return Task
        public void OBOUserAssertionHashUsernamePassedTest()
        {
            TokenCache cache = TokenCacheHelper.CreateCacheWithItems();
            string someAssertion = "some-assertion-passed-by-developer";
            TokenCacheKey key = cache.tokenCacheDictionary.Keys.First();

            //update cache entry with hash of an assertion that will not match
            cache.tokenCacheDictionary[key].UserAssertionHash =
                new CryptographyHelper().CreateSha256Hash(someAssertion);

            ConfidentialClientApplication app = new ConfidentialClientApplication(TestConstants.DefaultClientId,
                TestConstants.DefaultRedirectUri, new ClientCredential(TestConstants.DefaultClientSecret), new TokenCache());
            app.UserTokenCache = cache;

            //this is a fail safe. No call should go on network
            HttpMessageHandlerFactory.MockHandler = new MockHttpMessageHandler()
            {
                Method = HttpMethod.Post,
                ResponseMessage =
                    MockHelpers.CreateInvalidGrantTokenResponseMessage()
            };

            UserAssertion assertion = new UserAssertion(someAssertion, AssertionType, key.DisplayableId);
            Task<AuthenticationResult> task = app.AcquireTokenOnBehalfOfAsync(key.Scope.AsArray(),
                assertion, key.Authority, TestConstants.DefaultPolicy);
            AuthenticationResult result = task.Result;
            Assert.IsNotNull(result);
            Assert.AreEqual(key.UniqueId, result.User.UniqueId);
            Assert.AreEqual(key.DisplayableId, result.User.DisplayableId);
            Assert.AreEqual(HashAccessToken,
                cache.tokenCacheDictionary[key].UserAssertionHash);
        }
        public void OBOUserAssertionHashNotFoundTest()
        {
            TokenCache cache = TokenCacheHelper.CreateCacheWithItems();
            string someAssertion = "some-assertion-passed-by-developer";
            TokenCacheKey key = cache.tokenCacheDictionary.Keys.First();

            //update cache entry with hash of an assertion that will not match
            cache.tokenCacheDictionary[key].UserAssertionHash =
                new CryptographyHelper().CreateSha256Hash(someAssertion + "-but-not-in-cache");

            ConfidentialClientApplication app = new ConfidentialClientApplication(TestConstants.DefaultClientId,
                TestConstants.DefaultRedirectUri, new ClientCredential(TestConstants.DefaultClientSecret), new TokenCache());
            app.UserTokenCache = cache;

            string[] scope = {"mail.read"};
            HttpMessageHandlerFactory.MockHandler = new MockHttpMessageHandler()
            {
                Method = HttpMethod.Post,
                ResponseMessage =
                    MockHelpers.CreateSuccessTokenResponseMessage("unique_id_3", "*****@*****.**", "root_id_3",
                        scope)
            };

            UserAssertion assertion = new UserAssertion(someAssertion, AssertionType);
            Task<AuthenticationResult> task = app.AcquireTokenOnBehalfOfAsync(key.Scope.AsArray(),
                assertion, key.Authority, TestConstants.DefaultPolicy);
            AuthenticationResult result = task.Result;
            Assert.IsNotNull(result);
            Assert.AreEqual("unique_id_3", result.User.UniqueId);
            Assert.AreEqual("*****@*****.**", result.User.DisplayableId);

            //check for new assertion Hash
            AuthenticationResultEx resultEx =
                cache.tokenCacheDictionary.Values.First(r => r.Result.User.UniqueId.Equals("unique_id_3"));
            Assert.AreEqual(HashAccessToken, resultEx.UserAssertionHash);
        }