当前位置: 首页>>代码示例>>C#>>正文


C# ActiveDirectory.TokenCache类代码示例

本文整理汇总了C#中Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache的典型用法代码示例。如果您正苦于以下问题:C# TokenCache类的具体用法?C# TokenCache怎么用?C# TokenCache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


TokenCache类属于Microsoft.IdentityModel.Clients.ActiveDirectory命名空间,在下文中一共展示了TokenCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Authenticate

        public IAccessToken Authenticate(
            AzureAccount account,
            AzureEnvironment environment,
            string tenant,
            SecureString password,
            ShowDialog promptBehavior,
            TokenCache tokenCache,
            AzureEnvironment.Endpoint resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId)
        {
            var configuration = GetAdalConfiguration(environment, tenant, resourceId, tokenCache);

            TracingAdapter.Information(Resources.AdalAuthConfigurationTrace, configuration.AdDomain, configuration.AdEndpoint,
                configuration.ClientId, configuration.ClientRedirectUri, configuration.ResourceClientUri, configuration.ValidateAuthority);
            IAccessToken token;
            if (account.IsPropertySet(AzureAccount.Property.CertificateThumbprint))
            {
                var thumbprint = account.GetProperty(AzureAccount.Property.CertificateThumbprint);
                token = TokenProvider.GetAccessTokenWithCertificate(configuration, account.Id, thumbprint, account.Type);
            }
            else
            {

                token = TokenProvider.GetAccessToken(configuration, promptBehavior, account.Id, password, account.Type);
            }

            account.Id = token.UserId;
            return token;
        }
开发者ID:rohmano,项目名称:azure-powershell,代码行数:28,代码来源:AuthenticationFactory.cs

示例2: Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            //Redirect uri must match the redirect_uri used when requesting Authorization code.
            string redirectUri = Properties.Settings.Default.RedirectUrl;
            string authorityUri = "https://login.windows.net/common/oauth2/authorize/";
           
            // Get the auth code
            string code = Request.Params.GetValues(0)[0];
            
            // Get auth token from auth code       
            TokenCache TC = new TokenCache();

            AuthenticationContext AC = new AuthenticationContext(authorityUri, TC);

            ClientCredential cc = new ClientCredential
                (Properties.Settings.Default.ClientID,
                Properties.Settings.Default.ClientSecretKey);

            AuthenticationResult AR = AC.AcquireTokenByAuthorizationCode(code, new Uri(redirectUri), cc);

            //Set Session "authResult" index string to the AuthenticationResult
            Session["authResult"] = AR;

            //Redirect back to Default.aspx
            Response.Redirect("/Default.aspx");
        }
开发者ID:ChrisMBenson,项目名称:PowerBI-CSharp,代码行数:26,代码来源:redirect.aspx.cs

示例3: AcquireTokenHandlerBase

        protected AcquireTokenHandlerBase(Authenticator authenticator, TokenCache tokenCache, string resource, ClientKey clientKey, TokenSubjectType subjectType, bool callSync)
        {
            this.Authenticator = authenticator;
            this.CallState = CreateCallState(this.Authenticator.CorrelationId, callSync);
            Logger.Information(this.CallState, 
                string.Format("=== Token Acquisition started:\n\tAuthority: {0}\n\tResource: {1}\n\tClientId: {2}\n\tCacheType: {3}\n\tAuthentication Target: {4}\n\t",
                authenticator.Authority, resource, clientKey.ClientId,
                (tokenCache != null) ? tokenCache.GetType().FullName + string.Format(" ({0} items)", tokenCache.Count) : "null",
                subjectType));

            this.tokenCache = tokenCache;

            if (string.IsNullOrWhiteSpace(resource))
            {
                var ex = new ArgumentNullException("resource");
                Logger.Error(this.CallState, ex);
                throw ex;
            }

            this.Resource = (resource != NullResource) ? resource : null;
            this.ClientKey = clientKey;
            this.TokenSubjectType = subjectType;

            this.LoadFromCache = (tokenCache != null);
            this.StoreToCache = (tokenCache != null);
            this.SupportADFS = false;
        }
开发者ID:ankurchoubeymsft,项目名称:azure-activedirectory-library-for-dotnet,代码行数:27,代码来源:AcquireTokenHandlerBase.cs

示例4: AdalSilentTokenAcquisitionException

        async Task IAuthenticationProvider.AuthenticateRequestAsync(HttpRequestMessage request)
        {
            // look in the IBotDataBag for the token
            string objectIdentifier;
            string tenantID = null;
            byte[] tokenBlob = null;
            bool found
                = bag.TryGetValue(Keys.ObjectID, out objectIdentifier)
                && bag.TryGetValue(Keys.TenantID, out tenantID)
                && bag.TryGetValue(Keys.TokenCache, out tokenBlob);

            // if not found, then throw the exception that will restart the login flow
            if (! found)
            {
                throw new AdalSilentTokenAcquisitionException();
            }

            // deserialize the TokenCache and try to refresh the token silently
            var tokenCache = new TokenCache(tokenBlob);
            var token = await AcquireTokenSilentAsync(this.keys.ClientID, this.keys.ClientSecret, objectIdentifier, tenantID, tokenCache);

            // update the IBotDataBag with the new token if it's changed
            tokenBlob = tokenCache.Serialize();
            bag.SetValue(Keys.TokenCache, tokenBlob);

            // add the access token to the authentication header for the Microsoft Graph request
            var accessToken = token.AccessToken;
            request.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
        }
开发者ID:CHENShuang1994,项目名称:BotBuilder,代码行数:29,代码来源:AuthenticationProvider.cs

示例5: EnsureAuthenticationContext

        public async Task<bool> EnsureAuthenticationContext(string authority, bool show)
        {
            if (this.AuthenticationContext == null)
            {
                var cache = _cachePersist.Read();

                if (cache != null)
                {
                    var t = new TokenCache(cache);
                    this.AuthenticationContext = new AuthenticationContext(authority, t);
                }
                else
                {
                    this.AuthenticationContext = new AuthenticationContext(authority);
                }
            }
           
            var p = new PlatformParameters(show ? PromptBehavior.Always : PromptBehavior.Never, _deviceService.WindowHandle);
            this.AuthenticationResult =
                await this.AuthenticationContext.AcquireTokenAsync(
                    Office365ServicesUris.AADGraphAPIResourceId,
                    ClientId,
                    new Uri(RedirectUri),
                    p);

            var tokenCache = AuthenticationContext.TokenCache.Serialize();
            
            _cachePersist.Write(tokenCache);

            return !string.IsNullOrWhiteSpace(this.AuthenticationResult.AccessToken);
        }
开发者ID:jakkaj,项目名称:DayBar,代码行数:31,代码来源:AuthenticationHelper.cs

示例6: AcquireTokenSilentServiceErrorTestAsync

        public async Task AcquireTokenSilentServiceErrorTestAsync()
        {
            Sts sts = new AadSts();
            TokenCache cache = new TokenCache();
            TokenCacheKey key = new TokenCacheKey(sts.Authority, sts.ValidResource, sts.ValidClientId, TokenSubjectType.User, "unique_id", "[email protected]");
            cache.tokenCacheDictionary[key] = new AuthenticationResult("Bearer", "some-access-token",
                "invalid-refresh-token", DateTimeOffset.UtcNow);

            AuthenticationContext context = new AuthenticationContext(sts.Authority, sts.ValidateAuthority, cache);

            try
            {
                await context.AcquireTokenSilentAsync(sts.ValidResource, sts.ValidClientId, new UserIdentifier("unique_id", UserIdentifierType.UniqueId));
                Verify.Fail("AdalSilentTokenAcquisitionException was expected");
            }
            catch (AdalSilentTokenAcquisitionException ex)
            {
                Verify.AreEqual(AdalError.FailedToAcquireTokenSilently, ex.ErrorCode);
                Verify.AreEqual(AdalErrorMessage.FailedToRefreshToken, ex.Message);
                Verify.IsNotNull(ex.InnerException);
                Verify.IsTrue(ex.InnerException is AdalException);
                Verify.AreEqual(((AdalException)ex.InnerException).ErrorCode, "invalid_grant");
            }
            catch
            {
                Verify.Fail("AdalSilentTokenAcquisitionException was expected");
            }

        }
开发者ID:psignoret,项目名称:azure-activedirectory-library-for-dotnet,代码行数:29,代码来源:AcquireTokenSilentTests.cs

示例7: AcquireTokenHandlerBase

        protected AcquireTokenHandlerBase(Authenticator authenticator, TokenCache tokenCache, string[] scope,
            ClientKey clientKey, TokenSubjectType subjectType)
        {
            this.Authenticator = authenticator;
            this.CallState = CreateCallState(this.Authenticator.CorrelationId);
            PlatformPlugin.Logger.Information(this.CallState,
                string.Format(
                    "=== accessToken Acquisition started:\n\tAuthority: {0}\n\tResource: {1}\n\tClientId: {2}\n\tCacheType: {3}\n\tAuthentication Target: {4}\n\t",
                    authenticator.Authority, scope, clientKey.ClientId,
                    (tokenCache != null)
                        ? tokenCache.GetType().FullName + string.Format(" ({0} items)", tokenCache.Count)
                        : "null",
                    subjectType));

            this.tokenCache = tokenCache;
            this.ClientKey = clientKey;
            this.TokenSubjectType = subjectType;

            this.LoadFromCache = (tokenCache != null);
            this.StoreToCache = (tokenCache != null);
            this.SupportADFS = false;
            if (ADALScopeHelper.IsNullOrEmpty(scope))
            {
                throw new ArgumentNullException("scope");
            }

            this.Scope = scope;
            ValidateScopeInput(scope);
        }
开发者ID:dstrockis,项目名称:aad-msa-channel9-demo,代码行数:29,代码来源:AcquireTokenHandlerBase.cs

示例8: TokenCache

 static TokenCache()
 {
     DefaultShared = new TokenCache
     {
         BeforeAccess = PlatformPlugin.TokenCachePlugin.BeforeAccess,
         AfterAccess = PlatformPlugin.TokenCachePlugin.AfterAccess
     };
 }
开发者ID:dstrockis,项目名称:aad-msa-channel9-demo,代码行数:8,代码来源:TokenCache.cs

示例9: AcquireTokenNonInteractiveHandler

        public AcquireTokenNonInteractiveHandler(Authenticator authenticator, TokenCache tokenCache, string[] scope, string clientId, UserCredential userCredential)
            : base(authenticator, tokenCache, scope, new ClientKey(clientId), TokenSubjectType.User)
        {
            if (userCredential == null)
            {
                throw new ArgumentNullException("userCredential");
            }

            this.userCredential = userCredential;
        }
开发者ID:dstrockis,项目名称:aad-msa-channel9-demo,代码行数:10,代码来源:AcquireTokenNonInteractiveHandler.cs

示例10: UpdateTokenExpiryOnTokenCache

        public static void UpdateTokenExpiryOnTokenCache(TokenCache cache, DateTimeOffset newExpiry)
        {
            var cacheDictionary = cache.tokenCacheDictionary;

            var key = cacheDictionary.Keys.First();
            cache.tokenCacheDictionary[key].ExpiresOn = newExpiry; 
            var value = cacheDictionary.Values.First();
            cache.Clear();
            cacheDictionary.Add(key, value);        
        }
开发者ID:ankurchoubeymsft,项目名称:azure-activedirectory-library-for-dotnet,代码行数:10,代码来源:AdalFriend.cs

示例11: TokenCache

        static TokenCache()
        {
            DefaultShared = new TokenCache();

#if !ADAL_NET
            DefaultShared.BeforeAccess = DefaultTokenCache_BeforeAccess;
            DefaultShared.AfterAccess = DefaultTokenCache_AfterAccess;

            DefaultTokenCache_BeforeAccess(null);
#endif
        }
开发者ID:MscrmTools,项目名称:MscrmTools.Xrm.Connection,代码行数:11,代码来源:TokenCache.cs

示例12: AuthenticationContextProxy

        public AuthenticationContextProxy(string authority, bool validateAuthority, TokenCacheType tokenCacheType)
        {
            TokenCache tokenCache = null;
            if (tokenCacheType == TokenCacheType.InMemory)
            {
                tokenCache = new TokenCache();
            }

            this.context = new AuthenticationContext(authority, validateAuthority, tokenCache);
            this.context.CorrelationId = new Guid(FixedCorrelationId);
        }
开发者ID:ankurchoubeymsft,项目名称:azure-activedirectory-library-for-dotnet,代码行数:11,代码来源:AuthenticationContextProxy.cs

示例13: AuthenticateAndAddToPowerBI

        public void AuthenticateAndAddToPowerBI()
        {
            var tokenCache = new TokenCache();
            var authenticationContext = new AuthenticationContext(AuthorityUri, tokenCache);
            var result = authenticationContext.AcquireToken(ResourceUri, SelectedTenant.Client, new Uri(RedirectUri), PromptBehavior.RefreshSession);

            var workspace = Workspace.GetFor(result.AccessToken);

            var dataset = workspace.Datasets.GetByName(SelectedTenant.Dataset);
            workspace.Rows.Add(dataset, new Message { ResponseInMinutes = Random.Next(0, 45) });
        }
开发者ID:msdevno,项目名称:PowerBIDotNet,代码行数:11,代码来源:ViewModel.cs

示例14: EnsureAuthenticationContext

        public void EnsureAuthenticationContext(TokenCache tokenCache)
        {
            if (ClaimsPrincipal.Current != null)
            {
                var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
                var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                var tenantId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;

                this.AuthenticationContext = new AuthenticationContext(
                    String.Format("{0}/{1}", AuthenticationHelper.AuthorizationUri, tenantId),
                    tokenCache);
            }
        }
开发者ID:CherifSy,项目名称:PnP,代码行数:13,代码来源:AuthenticationHelper.cs

示例15: AcquireTokenOnBehalfHandler

        public AcquireTokenOnBehalfHandler(Authenticator authenticator, TokenCache tokenCache, string resource, ClientKey clientKey, UserAssertion userAssertion)
            : base(authenticator, tokenCache, resource, clientKey, TokenSubjectType.UserPlusClient)
        {
            if (userAssertion == null)
            {
                throw new ArgumentNullException("userAssertion");
            }

            this.userAssertion = userAssertion;
            this.DisplayableId = userAssertion.UserName;

            this.SupportADFS = true;
        }
开发者ID:varunpsr,项目名称:XamarinAzureAD,代码行数:13,代码来源:AcquireTokenOnBehalfHandler.cs


注:本文中的Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。