Exemplo n.º 1
0
            public async Task <AuthUserDTO> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await dataContext.Users.FirstOrDefaultAsync(x => x.UserName == request.UserName);

                if (user is null)
                {
                    throw new HttpContextException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var result = await signInManager.CheckPasswordSignInAsync(user, request.Password, false);

                if (result.Succeeded)
                {
                    var refreshToken = refreshTokenGenerator.Generate(user.UserName);

                    await authCookies.SendAuthCookies(user, refreshToken);

                    var key = "rid-" + Convert.ToBase64String(Encoding.UTF8.GetBytes(user.UserName));
                    await cache.SetRefreshToken(key, refreshToken);

                    return(new AuthUserDTO(user));
                }
                else
                {
                    throw new HttpContextException(HttpStatusCode.BadRequest, new { User = "******" });
                }
                throw new Exception("Server Error -Login");
            }
Exemplo n.º 2
0
            public async Task <AuthUserDTO> Handle(Command request, CancellationToken cancellationToken)
            {
                var isExist = await dataContext.Users.FirstOrDefaultAsync(x => x.UserName == request.UserName) != null;

                if (isExist)
                {
                    throw new HttpContextException(HttpStatusCode.BadRequest, new { User = "******" });
                }

                var user = new AppUser
                {
                    FirstName = request.FirstName,
                    LastName  = request.LastName,
                    UserName  = request.UserName,
                    Email     = request.Email
                };
                var registerResult = await userManager.CreateAsync(user, request.Password);

                var roleResult = await userManager.AddToRoleAsync(user, "Normal");

                if (registerResult.Succeeded && roleResult.Succeeded)
                {
                    var refreshToken = refreshTokenGenerator.Generate(user.UserName);

                    await authCookies.SendAuthCookies(user, refreshToken);

                    var key = "rid-" + Convert.ToBase64String(Encoding.UTF8.GetBytes(user.UserName));
                    await cache.SetRefreshToken(key, refreshToken);

                    return(new AuthUserDTO(user));
                }
                throw new Exception("Server Error - Register");
            }