コード例 #1
0
        public async Task <IActionResult> GetMembershipByMemberUserName(Guid groupId, string memberUserName)
        {
            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            var loggedOnUserIsGroupOwner = await _taskBookBusiness.IsUserGroupOwner(loggedOnUser.Id, groupId);

            if (!loggedOnUserIsGroupOwner)
            {
                return(NotFound());
            }

            User memberUser = await _identityBusiness.FindByNameAsync(memberUserName);

            if (memberUser == null)
            {
                return(NotFound());
            }

            UserGroup membership = await _taskBookBusiness.GetGroupMembership(memberUser.Id, groupId);

            if (membership == null)
            {
                return(NotFound());
            }

            var viewModel = _mapper.Map <GroupMembershipViewModel>(membership);

            return(Ok(viewModel));
        }
コード例 #2
0
        public async Task <IActionResult> GetProfile(string userName)
        {
            User user = await _identityBusiness.FindByNameAsync(userName);

            if (user == null)
            {
                return(NotFound());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (user.Id != loggedOnUser.Id)
            {
                return(Forbid());
            }

            var profile = _mapper.Map <ProfileViewModel>(loggedOnUser);

            return(Ok(profile));
        }
コード例 #3
0
        public async Task <IActionResult> Logon([FromBody] LogonParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

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

            SignInResult result = await _identityBusiness.PasswordSignInAsync(parameters.UserName, parameters.Password);

            if (!result.Succeeded)
            {
                return(Unauthorized());
            }

            var user = await _identityBusiness.FindByNameAsync(parameters.UserName);

            var claims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
            };

            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.SecurityKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(
                issuer: _jwtOptions.Issuer,
                audience: _jwtOptions.Audience,
                claims: claims,
                expires: DateTime.Now.AddMinutes(_jwtOptions.ExpiresMinutes),
                signingCredentials: creds);

            return(Ok(new
            {
                token = new JwtSecurityTokenHandler().WriteToken(token)
            }));
        }
コード例 #4
0
        public async Task <IActionResult> ChangePassword(
            [FromRoute] string userName,
            [FromBody] ChangePasswordParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

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

            User user = await _identityBusiness.FindByNameAsync(userName);

            if (user == null)
            {
                return(NotFound());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (user.Id != loggedOnUser.Id)
            {
                return(Forbid());
            }

            IdentityResult result = await _identityBusiness.ChangePasswordAsync(loggedOnUser, parameters.CurrentPassword, parameters.NewPassword);

            if (result.Succeeded)
            {
                return(NoContent());
            }

            foreach (IdentityError error in result.Errors)
            {
                ModelState.AddModelError(error.Code, error.Description);
            }

            return(UnprocessableEntity(ModelState));
        }