public async Task <AppUser> CreateAdmin(AppUser AppUser, string password)
        {
            var creater = new Passhash();

            AppUser.PasswordHash = creater.gethash(password);
            await _AppUser.InsertOneAsync(AppUser);

            return(AppUser);
        }
        public async Task <AppUser> CreateClient(AppUser AppUser, string password)
        {
            var creater = new Passhash();

            AppUser.PasswordHash = creater.gethash(password);
            _AppUser.InsertOne(AppUser);
            var ussr = _AppUser.Find <AppUser>(x => x.UserName == AppUser.UserName).FirstOrDefault();
            var id   = ussr.Id;
            await _accserv.CreateClientAccount(AppUser, id);

            return(AppUser);
        }
        public bool checkPass(string id, string pass)
        {
            var appuser = _AppUser.Find <AppUser>(AppUser => AppUser.Id == id).FirstOrDefault();
            var creater = new Passhash();
            var z       = creater.gethash(pass);

            if (z == appuser.PasswordHash)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public async Task <AppUser> UpdatePassword(string id, string newpass)
        {
            var creater    = new Passhash();
            var hashedpass = creater.gethash(newpass);

            var filter = Builders <AppUser> .Filter.Eq("Id", id);

            var update = Builders <AppUser> .Update.Set("PasswordHash", hashedpass);

            await _AppUser.UpdateOneAsync(filter, update);

            var result = await Get(id);

            return(result);
        }
Exemple #5
0
        public static void SeedUsers(IMongoCollection <AppUser> Appuser)
        {
            var clients = Appuser.CountDocuments(x => x.Roles[0] == "Admin");

            // var clients = Appuser.Find<AppUser>(AppUser => true).ToList();


            if (clients < 1)
            {
                string  pass    = "******";
                var     creater = new Passhash();
                AppUser admin   = new AppUser();
                admin.UserName = "******";
                admin.Email    = "*****@*****.**";
                admin.Roles.Add("Admin");
                admin.PasswordHash = creater.gethash(pass);
                admin.Name         = "Amir";
                admin.LastName     = "Nasser";
                Appuser.InsertOne(admin);
            }
        }
Exemple #6
0
        public async Task <AuthenticationResult> AuthenticateUser(AuthenticationReqest ar)
        {
            var user = _AppUser.Find <AppUser>(AppUser => AppUser.UserName == ar.Username).FirstOrDefault();

            if (user == null)
            {
                return(new AuthenticationResult
                {
                    Errors = new[] { "User With this username does not exist " }
                });
            }

            var  creater = new Passhash();
            var  Pass    = creater.gethash(ar.Password);
            bool result  = false;

            if (Pass == user.PasswordHash)
            {
                result = true;
            }
            // var f1 = Builders<AppUser>.Filter.Eq("PasswordHash",Pass);
            // var f2 = Builders<AppUser>.Filter.Eq("UserName",ar.Username);
            // var result = _AppUser.Find<AppUser>(AppUser => AppUser.UserName == Pass).FirstOrDefault();

            AuthenticationResult response = new AuthenticationResult();

            if (result == false)
            {
                return(new AuthenticationResult
                {
                    Errors = new[] { "Username/password Combination are wrong" }
                });
            }

            return(await GenerateAuthenticationResultForUserAsync(user));
        }
Exemple #7
0
        private async Task <AuthenticationResult> GenerateAuthenticationResultForUserAsync(AppUser user)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes(_jwtSettings.Secret);
            var userR        = _AppUser.Find <AppUser>(x => x.Id == user.Id).FirstOrDefault();
            var userrole     = userR.Roles;

            var claimlist = new List <Claim> {
                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                new Claim("id", userR.Id),
                new Claim("name", userR.UserName),
                new Claim("role", userrole.FirstOrDefault())
            };

            var claims = new List <IdentityUserClaim>();

            for (int i = 0; i < claimlist.Count; i++)
            {
                claims.Add(new IdentityUserClaim(claimlist[i]));
            }



            foreach (var userRole in userrole)
            {
                Claim claim = new Claim(ClaimTypes.Role, userRole);
                claims.Add(new IdentityUserClaim(claim));
            }



            var filter = Builders <AppUser> .Filter.Eq("Id", userR.Id);

            var userClaims    = _AppUser.Find <AppUser>(filter).FirstOrDefault();
            var CurrentClaims = userClaims.Claims;

            CurrentClaims.AddRange(claims);

            var update = Builders <AppUser> .Update.Set("Claims", CurrentClaims);

            await _AppUser.UpdateOneAsync(filter, update);


            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claimlist),
                Expires            = DateTime.Now.Add(_jwtSettings.TokenLifetime),
                SigningCredentials =
                    new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            var token   = tokenHandler.CreateToken(tokenDescriptor);
            var creater = new Passhash();

            var refreshToken = new RefreshToken
            {
                Token     = creater.gethash(token.Id),
                JwtId     = token.Id,
                AppUserID = user.Id,
                Created   = DateTime.Now,
                Expires   = DateTime.Now.AddMonths(6)
            };
            await _refresh.InsertOneAsync(refreshToken);


            return(new AuthenticationResult
            {
                Username = user.UserName,
                Success = true,
                Token = tokenHandler.WriteToken(token),
                RefreshToken = refreshToken.Token,
            });
        }