Пример #1
0
 public User(UserFasserly user, IJwtGenerator jwtGenerator, string refreshToken)
 {
     DisplayName  = user.DisplayName;
     Token        = jwtGenerator.CreateToken(user);
     Username     = user.UserName;
     Image        = user.Photos.FirstOrDefault(x => x.IsMain)?.Url;
     RefreshToken = refreshToken;
 }
Пример #2
0
        public async Task GetAllTrainigAsync()
        {
            var user = new UserFasserly
            {
                DisplayName = "Dali",
                Email       = "*****@*****.**",
                UserName    = "******"
            };

            _userManager.Setup(u => u.CreateAsync(user, "Pa$$W0rd"));

            //_mediator.Verify(x => x.Send(new Login.Query { Email = "", Password="" }));
            //var logged = await _mediator.Send(new Login.Query { Email = user.Email, Password = "******" });
        }
Пример #3
0
            public async Task <User> Handle(Query request, CancellationToken cancellationToken)
            {
                var userInfo = await _facebookAccessor.FacebookLogin(request.AccessToken);

                if (userInfo == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var user = await _userManager.FindByEmailAsync(userInfo.Email);

                var refreshToken = _jwtGenerator.GenerateRefreshToken();

                if (user != null)
                {
                    user.RefreshTokens.Add(refreshToken);
                    user.RefreshTokens.Add(refreshToken);
                    await _userManager.UpdateAsync(user);

                    return(new User(user, _jwtGenerator, refreshToken.Token));
                }


                user = new UserFasserly
                {
                    DisplayName    = userInfo.Name,
                    Id             = userInfo.Id,
                    UserName       = "******" + userInfo.Id,
                    Email          = userInfo.Email,
                    EmailConfirmed = true,
                };

                var photo = new Photo
                {
                    Id     = "fb_" + userInfo.Id,
                    Url    = userInfo.Picture.Data.Url,
                    IsMain = true,
                };

                user.Photos.Add(photo);

                var result = await _userManager.CreateAsync(user);

                if (!result.Succeeded)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                return(new User(user, _jwtGenerator, refreshToken.Token));
            }
Пример #4
0
        public string CreateToken(UserFasserly user)
        {
            var claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.NameId, user.UserName)
            };

            //genrate signing credentials
            var creds = new SigningCredentials(_key, SecurityAlgorithms.HmacSha256Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
Пример #5
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await context.Users.AnyAsync(x => x.Email == request.Email))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists" });
                }
                if (await context.Users.AnyAsync(x => x.UserName == request.Username))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Username = "******" });
                }

                var user = new UserFasserly
                {
                    UserName    = request.Username,
                    Email       = request.Email,
                    DisplayName = request.DisplayName,
                };

                var result = await _userManager.CreateAsync(user, request.Password);

                await _userManager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    throw new Exception("Problem creating user");
                }
                var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                //ENcode the token with webEncoder
                token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));
                var verifyUrl = $"{request.Origin}/user/verifyEmail?token={token}&email={request.Email}";
                var message   = $"<p>Please click the below link to verify you email adress: <a href='{verifyUrl}'>{verifyUrl}<a/></p>";
                await _eMailSender.SendEmailAsync(request.Email, "Verify adress", message);

                return(Unit.Value);
            }
Пример #6
0
 public UserViewModel(UserFasserly user)
 {
     Email    = user.Email;
     Password = user.PasswordHash;
 }