コード例 #1
0
        /// <summary>Attempts to retrieve the token for a user that's in a login flow.
        /// </summary>
        /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
        /// <param name="connectionName">Name of the auth connection to use.</param>
        /// <param name="magicCode">(Optional) Optional user entered code to validate.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Token Response or null if the token was not found.</returns>
        public virtual Task <TokenResponse> GetUserTokenAsync(ITurnContext turnContext, string connectionName, string magicCode, CancellationToken cancellationToken)
        {
            var key = new UserTokenKey()
            {
                ConnectionName = connectionName,
                ChannelId      = turnContext.Activity.ChannelId,
                UserId         = turnContext.Activity.From.Id,
            };

            if (magicCode != null)
            {
                var magicCodeRecord = _magicCodes.FirstOrDefault(x => key.Equals(x.Key));
                if (magicCodeRecord != null && magicCodeRecord.MagicCode == magicCode)
                {
                    // move the token to long term dictionary
                    AddUserToken(connectionName, key.ChannelId, key.UserId, magicCodeRecord.UserToken);
                    _magicCodes.Remove(magicCodeRecord);
                }
            }

            if (_userTokens.TryGetValue(key, out string token))
            {
                // found
                return(Task.FromResult(new TokenResponse()
                {
                    ConnectionName = connectionName,
                    Token = token,
                }));
            }
            else
            {
                // not found
                return(Task.FromResult <TokenResponse>(null));
            }
        }
コード例 #2
0
        /// <summary>
        /// Adds a fake user token so it can later be retrieved.
        /// </summary>
        /// <param name="connectionName">The connection name.</param>
        /// <param name="channelId">The channel id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="token">The token to store.</param>
        /// <param name="magicCode">The optional magic code to associate with this token.</param>
        public void AddUserToken(string connectionName, string channelId, string userId, string token, string magicCode = null)
        {
            var key = new UserTokenKey()
            {
                ConnectionName = connectionName,
                ChannelId      = channelId,
                UserId         = userId,
            };

            if (magicCode == null)
            {
                if (_userTokens.ContainsKey(key))
                {
                    _userTokens[key] = token;
                }
                else
                {
                    _userTokens.Add(key, token);
                }
            }
            else
            {
                _magicCodes.Add(new TokenMagicCode()
                {
                    Key       = key,
                    MagicCode = magicCode,
                    UserToken = token,
                });
            }
        }
コード例 #3
0
 public async Task <UserToken> GetByKeyAsync(UserTokenKey key)
 {
     return(await Connection.QuerySingleOrDefaultAsync <UserToken>(
                sql :
                @"
             SELECT
                 [UserId],
                 [LoginProvider],
                 [Name],
                 [Value]
             FROM
                 [UserTokens]
             WHERE
                 [UserId] = @UserId
                 AND [LoginProvider] = @LoginProvider
                 AND [Name] = @Name
         ",
                param : new
     {
         UserId = key.UserId,
         LoginProvider = key.LoginProvider,
         Name = key.Name
     },
                transaction : Transaction
                ));
 }
コード例 #4
0
        public async Task SetTokenAsync(DemoIdentityUser user, string loginProvider, string name, string value, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            throwIfDisposed();
            ExceptionUtil.ThrowIfNull(user, nameof(user));

            var userId       = getGuid(user.Id);
            var userTokenKey = new UserTokenKey {
                LoginProvider = loginProvider, Name = name, UserId = userId
            };
            var userToken = await _unitOfWork.UserTokenRepository.GetByKeyAsync(userTokenKey);

            if (userToken == null)
            {
                userToken = new UserToken
                {
                    LoginProvider = loginProvider,
                    Name          = name,
                    UserId        = userId,
                    Value         = value
                };
                await _unitOfWork.UserTokenRepository.AddAsync(userToken);
            }
            else
            {
                userToken.Value = value;
                await _unitOfWork.UserTokenRepository.UpdateAsync(userToken);
            }

            await _unitOfWork.CommitAsync();
        }
コード例 #5
0
        public async Task RemoveTokenAsync(DemoIdentityUser user, string loginProvider, string name, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            throwIfDisposed();
            ExceptionUtil.ThrowIfNull(user, nameof(user));
            var userId = getGuid(user.Id);

            var tokenKey = new UserTokenKey {
                LoginProvider = loginProvider, Name = name, UserId = userId
            };
            var token = await _unitOfWork.UserTokenRepository.GetByKeyAsync(tokenKey);

            if (token != null)
            {
                await _unitOfWork.UserTokenRepository.DeleteAsync(token);

                await _unitOfWork.CommitAsync();
            }
        }
コード例 #6
0
 public UserToken GetByKey(UserTokenKey key)
 {
     return(GetByKeyAsync(key).Result);
 }