コード例 #1
0
ファイル: UserService.cs プロジェクト: mek-12/mgc-project
        public CResult <string> Authenticate(UserCredentials userCredentials)
        {
            try
            {
                using (var _userManager = new UserManager())
                    using (var _roleManager = new RoleManager())
                        using (var _userRoleManager = new UserRoleManager())
                        {
                            var user = _userManager.GetWithCondition(
                                c => c.UserName == userCredentials.UserName &&
                                c.Password == userCredentials.Password).Model;

                            var userRole = _userRoleManager.GetWithCondition(c => c.UserId == user.Id).Model;

                            if (userRole == null)
                            {
                                return new CResult <string>()
                                       {
                                           Data = string.Empty, IsSucceeded = false, Message = "Role undefined"
                                       }
                            }
                            ;

                            var role     = _roleManager.GetWithCondition(c => c.Id == userRole.RoleId).Model;
                            var userInfo = new UserInfo()
                            {
                                FirstName      = user.FirstName,
                                LastName       = user.LastName,
                                HasAdminRights = false
                            };

                            var accessTokenResult = jwtTokenGenerator.GenerateAccessTokenWithClaimsPrincipal(
                                userCredentials.UserName,
                                role.Name,
                                AddClaims(userInfo));

                            return(new CResult <string>()
                            {
                                Data = accessTokenResult.AccessToken, Message = "Token Created", IsSucceeded = true
                            });
                        }
            }
            catch (Exception ex)
            {
                return(new CResult <string>()
                {
                    Data = string.Empty, IsSucceeded = false, Message = ex.Message
                });
            }
        }