예제 #1
0
        public async Task <IHttpActionResult> Login([FromBody] LoginViewModel model)
        {
            if (model == null)
            {
                model = new LoginViewModel();
                Validate(model);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Verify the capcha first.
            var bIsCaptchaValid =
                await _captchaService.IsCaptchaValidAsync(model.ClientCaptchaCode, null, CancellationToken.None);

            if (!bIsCaptchaValid)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden,
                                                                   HttpMessages.CaptchaInvalid)));
            }

            // Get profile from system.
            var profile = await _userService.LoginAsync(model, CancellationToken.None);

            ; // User is not found.
            if (profile == null)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpMessages.UserNotFound)));
            }

            // Initialize access token.
            var token = new TokenViewModel();

            token.LifeTime = 3600;
            token.Type     = "Bearer";

            if (string.IsNullOrWhiteSpace(profile.AccessToken))
            {
                // Add expired time.
                var expiredAt = DateTime.UtcNow.AddSeconds(token.LifeTime);

                var payload = new Dictionary <string, string>();
                payload.Add(ClaimTypes.Email, profile.Email);
                payload.Add(ClaimTypes.Name, $"{profile.FirstName} {profile.LastName}");
                payload.Add(ClaimTypes.Expired, expiredAt.ToString("yyyy/MM/dd"));
                profile.AccessToken = token.AccessToken = _tokenService.Encode(payload);
                _profileCacheService.Add(profile.Email, profile, token.LifeTime);
            }
            else
            {
                token.AccessToken = profile.AccessToken;
            }

            return(Ok(token));
        }
예제 #2
0
        public async Task <IActionResult> BasicLogin([FromBody] LoginViewModel model)
        {
            #region Parameters validation

            // Parameter hasn't been initialized.
            if (model == null)
            {
                model = new LoginViewModel();
                TryValidateModel(model);
            }

            // Invalid modelstate.
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            #endregion

            // Verify the captcha.
            var bIsCaptchaValid =
                await _captchaService.IsCaptchaValidAsync(model.CaptchaCode, null, CancellationToken.None);

            if (!bIsCaptchaValid)
            {
                return(StatusCode((int)HttpStatusCode.Forbidden, new ApiResponse(HttpMessages.CaptchaInvalid)));
            }

            var user = await _userDomain.LoginAsync(model);

            // Initialize jwt token.
            var jsonWebToken = _userDomain.GenerateJwt(user);

            return(Ok(jsonWebToken));
        }