public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var container = new UnityContainer();

            container.RegisterType <IRefreshTokenDataSource, RefreshTokenDataSource>();
            container.RegisterType <IRefreshTokenService, RefreshTokenService>();
            IRefreshTokenService _refreshTokenService = container.Resolve <IRefreshTokenService>();

            var refreshTokenLifeTime = context.OwinContext.Get <string>("clientRefreshTokenLifeTime") ?? "60";

            var token = new FM.Portal.Core.Model.RefreshToken()
            {
                ID         = Guid.NewGuid(),
                UserID     = Guid.Parse(context.Ticket.Identity.GetUserId()),
                IssuedDate = DateTime.UtcNow,
                ExpireDate = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime))
            };

            context.Ticket.Properties.IssuedUtc  = token.IssuedDate;
            context.Ticket.Properties.ExpiresUtc = token.ExpireDate;

            token.ProtectedTicket = context.SerializeTicket();

            var result = _refreshTokenService.Add(token);

            if (result.Success)
            {
                context.SetToken(token.ID.ToString("n"));
            }
        }
        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            // Initilize refresh token service
            this._refreshTokenService = GetRefreshTokenService(new DbFactory()
            {
            });

            //Get the client Id from Ticket properties
            var clientId = context.Ticket.Properties.Dictionary[Constant.OAuthorization_Properties_ClientId];

            if (string.IsNullOrEmpty(clientId))
            {
                return;
            }

            var userName = context.Ticket.Identity.FindFirst(ClaimTypes.NameIdentifier);

            if (userName == null)
            {
                return;
            }

            // Generating a Unique Refresh Token ID
            var refreshTokenId = Guid.NewGuid().ToString("n");

            var refreshTokenLifeTime = context.OwinContext.Get <string>(Constant.OAuthorization_OAuth_ClientRefreshTokenLifeTime);
            // Creating the Refresh Token object
            var refreshToken = new RefreshToken()
            {
                // storing the refreshTokenId in hash format
                ID          = CipherUtility.GetHash(refreshTokenId),
                ClientId    = clientId,
                UserName    = userName.Value,
                IssuedTime  = DateTime.UtcNow,
                ExpiredTime = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime))
            };

            // Setting Issued and Expired time of the refresh token
            context.Ticket.Properties.IssuedUtc  = refreshToken.IssuedTime;
            context.Ticket.Properties.ExpiresUtc = refreshToken.ExpiredTime;

            refreshToken.ProtectedTicket = context.SerializeTicket();

            var result = _refreshTokenService.Add(refreshToken);

            if (result)
            {
                context.SetToken(refreshTokenId);
            }
        }
Example #3
0
        public IDataResult <RefreshToken> CreateRefreshToken(Token token, int userId)
        {
            var refreshToken = _refreshTokenService.GetByUserId(userId).Data;

            if (refreshToken == null)
            {
                _refreshTokenService.Add(new RefreshToken {
                    UserId = userId, Token = token.RefreshToken, Expiration = token.RefreshTokenExpiration
                });
            }
            else
            {
                refreshToken.UserId     = userId;
                refreshToken.Token      = token.RefreshToken;
                refreshToken.Expiration = token.RefreshTokenExpiration;
                _refreshTokenService.Update(refreshToken);
            }

            return(new SuccessDataResult <RefreshToken>(refreshToken));
        }