Ejemplo n.º 1
0
        public async Task <IActionResult> Login(SignInViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await userManager.FindByNameAsync(model.UserName);

            if (user is null)
            {
                user = await userManager.FindByEmailAsync(model.UserName);

                if (user is null)
                {
                    ModelState.AddModelError(nameof(model.UserName), "نام کاربری اشتباه است");
                }
            }

            var IsPasswordValid = await userManager.CheckPasswordAsync(user, model.Password);

            if (!IsPasswordValid)
            {
                ModelState.AddModelError(nameof(model.Password), "رمز عبور اشتباه است");
            }

            if (ModelState.IsValid)
            {
                var myIp  = GetCurrentIpAddressExtention.Get(HttpContext);
                var token = await jwtService.GenerateAsync(user);

                var RefreshtokenGuid = await generateResreshTokenService.Generate(user, myIp);

                NameValueCollection datacollection = new NameValueCollection();
                datacollection.Add("token", token);
                datacollection.Add("refreshtoken", Convert.ToString(RefreshtokenGuid));
                var urlCallBack = decryptService.Decrypt(model.UCB);
                var form        = FormPostExtention.PreparePostForm(urlCallBack, datacollection);
                return(Content(form, "text/html"));
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <ApiResult <TokensDTO> > RefreshToken(Guid refreshToken)
        {
            var myIp = GetCurrentIpAddressExtention.Get(HttpContext);

            var refreshTokenModel = await getRefreshTokenService.GetByRefreshToken(refreshToken);

            if (refreshTokenModel is null || refreshTokenModel.IsExpired)
            {
                return(BadRequest());
            }
            var newRefreshToken = await updateResreshTokenService.Update(refreshToken, myIp);

            var user = await userManager.FindByIdAsync(refreshTokenModel?.UserId);

            var newJWT = await jwtService.GenerateAsync(user);

            return(Ok(new TokensDTO()
            {
                token = newJWT, refreshToken = newRefreshToken
            }));
        }