Esempio n. 1
0
        private async Task <JsonResult> SaveImpersonationTokenAndGetTargetUrl(int?tenantId, long userId, bool isBackToImpersonator)
        {
            ImpersonationCacheItem impersonationCacheItem = new ImpersonationCacheItem(tenantId, userId, isBackToImpersonator);

            if (!isBackToImpersonator)
            {
                impersonationCacheItem.ImpersonatorTenantId = this.AbpSession.TenantId;
                impersonationCacheItem.ImpersonatorUserId   = this.AbpSession.GetUserId();
            }
            string str = Guid.NewGuid().ToString();

            await this._cacheManager.GetImpersonationCache().SetAsync(str, impersonationCacheItem, new TimeSpan?(TimeSpan.FromMinutes(1)));

            string tenancyName = null;

            if (tenantId.HasValue)
            {
                Tenant byIdAsync = await this._tenantManager.GetByIdAsync(tenantId.Value);

                tenancyName = byIdAsync.TenancyName;
            }
            string str1 = string.Concat(this._webUrlService.GetSiteRootAddress(tenancyName), "Account/ImpersonateSignIn?tokenId=", str);

            return(this.Json(new MvcAjaxResponse()
            {
                TargetUrl = str1
            }));
        }
Esempio n. 2
0
        public virtual async Task <ActionResult> ImpersonateSignIn(string tokenId)
        {
            ActionResult           action;
            ImpersonationCacheItem orDefaultAsync = await this._cacheManager.GetImpersonationCache().GetOrDefaultAsync(tokenId);

            ImpersonationCacheItem impersonationCacheItem = orDefaultAsync;

            if (impersonationCacheItem == null)
            {
                throw new UserFriendlyException(this.L("ImpersonationTokenErrorMessage"));
            }
            using (IDisposable disposable = this._unitOfWorkManager.Current.SetFilterParameter("MayHaveTenant", "tenantId", impersonationCacheItem.TargetTenantId))
            {
                FuelWerx.Authorization.Users.User user = await this._userManager.FindByIdAsync(impersonationCacheItem.TargetUserId);

                ClaimsIdentity claimsIdentity = await this._userManager.CreateIdentityAsync(user, "ApplicationCookie");

                if (!impersonationCacheItem.IsBackToImpersonator)
                {
                    if (impersonationCacheItem.ImpersonatorTenantId.HasValue)
                    {
                        int value = impersonationCacheItem.ImpersonatorTenantId.Value;
                        claimsIdentity.AddClaim(new Claim("http://www.aspnetboilerplate.com/identity/claims/impersonatorTenantId", value.ToString(CultureInfo.InvariantCulture)));
                    }
                    long impersonatorUserId = impersonationCacheItem.ImpersonatorUserId;
                    claimsIdentity.AddClaim(new Claim("http://www.aspnetboilerplate.com/identity/claims/impersonatorUserId", impersonatorUserId.ToString(CultureInfo.InvariantCulture)));
                }
                this.AuthenticationManager.SignOut(new string[] { "ApplicationCookie" });
                IAuthenticationManager   authenticationManager  = this.AuthenticationManager;
                AuthenticationProperties authenticationProperty = new AuthenticationProperties()
                {
                    IsPersistent = false
                };
                authenticationManager.SignIn(authenticationProperty, new ClaimsIdentity[] { claimsIdentity });
                await this._cacheManager.GetImpersonationCache().RemoveAsync(tokenId);

                action = this.RedirectToAction("Index", "Application");
            }
            return(action);
        }
        private async Task <JsonResult> SaveImpersonationTokenAndGetTargetUrl(int?tenantId, long userId, bool isBackToImpersonator)
        {
            using (UnitOfWorkManager.Current.SetTenantId(tenantId))
            {
                //Create a cache item
                var cacheItem = new ImpersonationCacheItem(
                    tenantId,
                    userId,
                    isBackToImpersonator
                    );

                if (!isBackToImpersonator)
                {
                    cacheItem.ImpersonatorTenantId = AbpSession.TenantId;
                    cacheItem.ImpersonatorUserId   = AbpSession.GetUserId();
                }

                //Create a random token and save to the cache
                var tokenId = Guid.NewGuid().ToString();
                await _cacheManager
                .GetImpersonationCache()
                .SetAsync(tokenId, cacheItem, TimeSpan.FromMinutes(1));

                //Find tenancy name
                string tenancyName = null;
                if (tenantId.HasValue)
                {
                    tenancyName = (await _tenantManager.GetByIdAsync(tenantId.Value)).TenancyName;
                }

                //Create target URL
                var targetUrl = _webUrlService.GetSiteRootAddress(tenancyName) + "Account/ImpersonateSignIn?tokenId=" + tokenId;
                return(Json(new MvcAjaxResponse {
                    TargetUrl = targetUrl
                }));
            }
        }
Esempio n. 4
0
        private async Task <string> GenerateImpersonationTokenAsync(int?tenantId, long userId, bool isBackToImpersonator)
        {
            //Create a cache item
            var cacheItem = new ImpersonationCacheItem(
                tenantId,
                userId,
                isBackToImpersonator
                );

            if (!isBackToImpersonator)
            {
                cacheItem.ImpersonatorTenantId = AbpSession.TenantId;
                cacheItem.ImpersonatorUserId   = AbpSession.GetUserId();
            }

            //Create a random token and save to the cache
            var token = Guid.NewGuid().ToString();

            await _cacheManager
            .GetImpersonationCache()
            .SetAsync(token, cacheItem, TimeSpan.FromMinutes(1));

            return(token);
        }