Пример #1
0
        public async Task CreateToken(
            IUserToken userToken,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (userToken == null)
            {
                throw new ArgumentException("userToken can't be null");
            }
            if (userToken.LoginProvider.Length == -1)
            {
                throw new ArgumentException("userToken must have a loginprovider");
            }
            if (userToken.Name.Length == -1)
            {
                throw new ArgumentException("userToken must have a Name");
            }
            if (userToken.UserId == Guid.Empty)
            {
                throw new ArgumentException("userToken must have a user id");
            }

            var token = UserToken.FromIUserToken(userToken);

            using (var dbContext = _contextFactory.CreateContext())
            {
                dbContext.UserTokens.Add(token);

                int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken)
                                   .ConfigureAwait(false);
            }
        }
Пример #2
0
        public async Task UpdateToken(
            IUserToken userToken,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ThrowIfDisposed();
            cancellationToken.ThrowIfCancellationRequested();

            if (userToken == null)
            {
                throw new ArgumentException("userToken can't be null");
            }
            if (userToken.LoginProvider.Length == -1)
            {
                throw new ArgumentException("userToken must have a loginprovider");
            }
            if (userToken.Name.Length == -1)
            {
                throw new ArgumentException("userToken must have a Name");
            }
            if (userToken.UserId == Guid.Empty)
            {
                throw new ArgumentException("userToken must have a user id");
            }

            var projectId = userToken.SiteId.ToString();

            var token = UserToken.FromIUserToken(userToken);

            // this will be a tricky one for queries because the key consists of 4 columns
            // TODO: review this and whether we really need all the  parts of the key in EF
            // http://www.jerriepelser.com/blog/using-aspnet-oauth-providers-without-identity
            // ProviderKey is the unique key associated with the login on that service
            var key = token.UserId.ToString()
                      + "~" + token.SiteId.ToString()
                      + "~" + token.LoginProvider
                      + "~" + token.Name;

            await tokenCommands.UpdateAsync(
                projectId,
                key,
                token,
                cancellationToken).ConfigureAwait(false);
        }
Пример #3
0
        public async Task UpdateToken(
            IUserToken userToken,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (userToken == null)
            {
                throw new ArgumentException("userToken can't be null");
            }
            if (userToken.LoginProvider.Length == -1)
            {
                throw new ArgumentException("userToken must have a loginprovider");
            }
            if (userToken.Name.Length == -1)
            {
                throw new ArgumentException("userToken must have a Name");
            }
            if (userToken.UserId == Guid.Empty)
            {
                throw new ArgumentException("userToken must have a user id");
            }

            var token = UserToken.FromIUserToken(userToken);

            using (var dbContext = _contextFactory.CreateContext())
            {
                bool tracking = dbContext.ChangeTracker.Entries <UserToken>().Any(x =>
                                                                                  x.Entity.SiteId == token.SiteId &&
                                                                                  x.Entity.UserId == token.UserId &&
                                                                                  x.Entity.LoginProvider == token.LoginProvider &&
                                                                                  x.Entity.Name == token.Name
                                                                                  );

                if (!tracking)
                {
                    dbContext.UserTokens.Update(token);
                }

                int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken)
                                   .ConfigureAwait(false);
            }
        }