コード例 #1
0
        public AuthenticationReponse Login(string username, string password)
        {
            AuthenticationReponse authenticationReponse = new AuthenticationReponse();

            User user = Users.FirstOrDefault(x => x.Email.ToLower().Equals(username) && x.Password.Equals(password));

            if (user != null)
            {
                var claims = new List <Claim>()
                {
                    new Claim(ClaimTypes.Email, user.Email),
                    new Claim(ClaimTypes.Role, user.Role.ToString())
                };

                authenticationReponse.AccessToken     = JwtService.GenerateToken(claims);
                authenticationReponse.IsAuthenticated = true;
            }
            else
            {
                authenticationReponse.IsAuthenticated = false;
                authenticationReponse.Error           = AppResources.LoginFailedErrorMessageLabel;
            }

            return(authenticationReponse);
        }
        public async Task <AuthenticationReponse> Login(User user)
        {
            var uri = $"{ApiConfiguration.GetBaseUrl()}/User/login";

            var userJson = JsonUtils.Serialize(user);

            HttpResponseMessage response;

            try
            {
                Log.Debug($"POST request, URI = {uri}");
                response = await _httpClient.PostAsync(uri, new StringContent(userJson, Encoding.UTF8, "application/json"));
            } catch (Exception ex)
            {
                Log.Error(ex, "POST User/login failed");
                return(new AuthenticationReponse()
                {
                    WasAuthenticationCorrect = false
                });
            }

            Token token = null;
            var   authenticationResponse = new AuthenticationReponse()
            {
                WasAuthenticationCorrect = response.IsSuccessStatusCode
            };

            if (response.IsSuccessStatusCode)
            {
                Log.Debug("POST User/login success");
                token = JsonUtils.Deserialize <Token>(await response.Content.ReadAsStringAsync());

                authenticationResponse.Roles     = token?.Roles.ToList();
                authenticationResponse.PatientId = token?.UserId;
                authenticationResponse.Token     = token?.TokenString;

                AuthenticationUtils.SaveUserToSession(_httpContextAccessor.HttpContext, authenticationResponse);
            }
            else
            {
                Log.Error($"POST User/login failed, status code = {response.StatusCode}");
            }

            return(authenticationResponse);
        }
コード例 #3
0
        public static void SaveUserToSession(HttpContext httpContext, AuthenticationReponse authenticationReponse)
        {
            if (authenticationReponse.WasAuthenticationCorrect)
            {
                httpContext.Session.SetString(tokenSessionKey, authenticationReponse.Token);

                if (authenticationReponse.Roles != null || authenticationReponse.Roles.Count > 0)
                {
                    string roles = string.Join(",", authenticationReponse.Roles);
                    httpContext.Session.SetString(rolesSessionKey, roles);
                }

                if (authenticationReponse.PatientId.HasValue)
                {
                    httpContext.Session.SetInt32(patientIdSessionKey, authenticationReponse.PatientId.Value);
                }
            }
        }
コード例 #4
0
        public async Task OnPostAsync_Incorrect_Login()
        {
            //// Arrange
            var authenticationResponse = new AuthenticationReponse()
            {
                WasAuthenticationCorrect = false
            };

            var authenticationServiceMock = new Mock <IAuthenticationService>();

            authenticationServiceMock.Setup(x => x.Login(It.IsAny <User>())).ReturnsAsync(authenticationResponse);

            var localizerMock = new Mock <ICultureLocalizer>();

            localizerMock.Setup(x => x.Text(It.IsAny <string>())).Returns(new LocalizedString("name", "localizedMessage"));

            var loginModel = new LoginModel(localizerMock.Object, authenticationServiceMock.Object)
            {
                LoginForm = new _LoginForm()
                {
                    Username = "******", Password = "******"
                }
            };
            var httpContext   = new DefaultHttpContext();
            var modelState    = new ModelStateDictionary();
            var actionContext = new ActionContext(httpContext, new RouteData(), new PageActionDescriptor(), modelState);
            var pageContext   = new PageContext(actionContext);

            loginModel.PageContext = pageContext;

            //// Act
            await loginModel.OnPostAsync();

            //// Assert
            Assert.Equal("localizedMessage", loginModel.Msg);
        }