Beispiel #1
0
        //public async Task<IActionResult> Register(string firstName, string lastName, string username, string password, string email)
        public async Task <IActionResult> Register(RegisterInfo registerInfo)
        {
            IdentityResult result;

            try
            {
                AppUser user = await UserMgr.FindByNameAsync(registerInfo.UserName);

                if (user == null)
                {
                    user = new AppUser
                    {
                        UserName  = registerInfo.UserName,
                        Email     = registerInfo.Email,
                        FirstName = registerInfo.FirstName,
                        LastName  = registerInfo.LastName
                    };

                    result = await UserMgr.CreateAsync(user, registerInfo.Password);
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
            }

            return(RedirectToAction("Login", "Account"));
        }
        public async Task <IActionResult> Register()
        {
            try
            {
                ViewBag.Message = "User already register";
                AppUser user = await UserMgr.FindByNameAsync("TestUser");

                if (user == null)
                {
                    user           = new AppUser();
                    user.UserName  = "******";
                    user.Email     = "*****@*****.**";
                    user.FirstName = "Nguyen";
                    user.LastName  = "Son";

                    IdentityResult result = await UserMgr.CreateAsync(user, "Admin123!");

                    ViewBag.Message = "User was created";
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
            }
            return(View());
        }
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            try
            {
                ViewBag.Message = "Toks vartotojo vardas jau registruotas";
                User usr = await UserMgr.FindByNameAsync(model.Email);

                if (usr == null)
                {
                    usr = new User
                    {
                        UserName = model.Email,
                        Email    = model.Email
                    };

                    IdentityResult result = await UserMgr.CreateAsync(usr, model.Password);

                    if (result.Succeeded)
                    {
                        ViewBag.Message = "Vartotojas sukurtas";
                    }
                    else
                    {
                        ViewBag.Message = result.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
            }
            return(View());
        }
Beispiel #4
0
        public async Task <IActionResult> Register(MergeModel user)
        {
            try
            {
                ViewBag.Message = "User already registered.";
                AppUser appuser = await UserMgr.FindByNameAsync(user.AppUsers.UserName);

                if (appuser == null)
                {
                    appuser           = new AppUser();
                    appuser.UserName  = user.AppUserName;
                    appuser.Email     = user.AppUserEmail;
                    appuser.FirstName = user.AppUsers.FirstName;
                    appuser.LastName  = user.AppUsers.LastName;

                    IdentityResult result = await UserMgr.CreateAsync(appuser, user.AppUserPassword);

                    ViewBag.Message = "User was successfully created!";

                    return(RedirectToAction("Login"));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
            }

            return(View());
        }
Beispiel #5
0
        public async Task <IActionResult> Login(string username, string password)
        {
            if (ModelState.IsValid)
            {
                var user = await UserMgr.FindByNameAsync(username);

                if (user != null && !user.EmailConfirmed &&
                    (await UserMgr.CheckPasswordAsync(user, password)))
                {
                    ModelState.AddModelError(string.Empty, "Email ainda não confirmado");
                    return(View());
                }

                var result = await SignInMgr.PasswordSignInAsync(username,
                                                                 password, false, false);

                if (result.Succeeded)
                {
                    return(RedirectToAction("index", "home"));
                }

                ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
            }

            return(View());
        }
Beispiel #6
0
        public async Task <string> getMethod()
        {
            var Message = "";

            try
            {
                Message = "User already registered";

                AppUser user = await UserMgr.FindByNameAsync("nhoarau");

                if (user == null)
                {
                    user           = new AppUser();
                    user.UserName  = "******";
                    user.Email     = "*****@*****.**";
                    user.FirstName = "Nathan";
                    user.LastName  = "HOARAU";

                    IdentityResult result = await UserMgr.CreateAsync(user, "PhilDougTest33*");

                    Message = "User was created";
                }
            }
            catch (Exception ex)
            {
                Message = ex.Message;
            }

            return(Message);
        }
        public async Task <IActionResult> Register(RegisterViewModel userForm)
        {
            try
            {
                ViewBag.Message = "User already registered";

                AppUser user = await UserMgr.FindByNameAsync(userForm.UserName);

                if (user == null)
                {
                    user           = new AppUser();
                    user.UserName  = userForm.UserName;
                    user.Email     = userForm.Email;
                    user.FirstName = userForm.FirstName;
                    user.LastName  = userForm.LastName;

                    IdentityResult result = await UserMgr.CreateAsync(user, userForm.Password);

                    if (result.Succeeded)
                    {
                        ViewBag.Message = "User was created";
                    }
                    else
                    {
                        ViewBag.Message = result.Errors;
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
            }

            return(View());
        }
Beispiel #8
0
        public async Task <IActionResult> GenerateToken([FromBody] JWTViewModel m)
        {
            if (ModelState.IsValid)
            {
                AppUser user = await UserMgr.FindByNameAsync(m.UserName);

                if (user != null)
                {
                    var signInResult = await SignInMgr.CheckPasswordSignInAsync(user, m.Password.ToString(), false);

                    if (signInResult.Succeeded)
                    {
                        var key  = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSettings.JWT_Secret));
                        var cred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                        var claims = new[]
                        {
                            new Claim(JwtRegisteredClaimNames.Sub, m.UserName),
                            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                            new Claim("UserType", user.Type),
                            new Claim("UserName", m.UserName)
                        };

                        var token = new JwtSecurityToken(

                            JWT.Iss,
                            JWT.Aud,
                            claims,
                            expires: DateTime.UtcNow.AddMinutes(30),
                            signingCredentials: cred

                            );

                        var results = new
                        {
                            token      = new JwtSecurityTokenHandler().WriteToken(token),
                            expiration = token.ValidTo
                        };
                        await LogChangeAsync(user, "Login");

                        return(Ok(results));
                    }
                    else
                    {
                        var err2 = new { status = "error", message = "Authentication Failed ! Check UserName & Password" };
                        return(BadRequest(err2));
                    }
                }

                var err = new { status = "error", message = "Could not find a user!" };
                return(BadRequest(err));
            }

            return(BadRequest());
        }
Beispiel #9
0
        public async Task <IActionResult> GetUser(string userName)
        {
            var user = await UserMgr.FindByNameAsync(userName);

            if (user != null)
            {
                return(Ok(user));
            }

            return(NotFound($"User with Id of {userName} does not exist"));
        }
Beispiel #10
0
        public async Task <IActionResult> VerifyPassword(string password)
        {
            if (User?.Identity.IsAuthenticated == true)
            {
                var username = User?.Identity.Name;
                var user     = await UserMgr.FindByNameAsync(username);

                return(Ok(UserMgr.CheckPasswordAsync(user, password).Result));
            }
            return(Ok(false));
        }
Beispiel #11
0
        public async Task <IActionResult> Register(RegisterViewModel usermodel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    AppUser user = await UserMgr.FindByNameAsync(usermodel.Username);

                    if (user == null)
                    {
                        user = new AppUser
                        {
                            UserName = usermodel.Username,
                            Email    = usermodel.Email,
                            FullName = usermodel.FullName,
                            Address  = usermodel.Address,
                            Type     = "Customer"
                        };

                        IdentityResult result = await UserMgr.CreateAsync(user, usermodel.Password);

                        //await UserMgr.AddToRoleAsync(user, user.Type);
                        if (result.Succeeded)
                        {
                            await LogChangeAsync(user, "REGISTER");

                            return(Created("", usermodel));
                        }
                        else
                        {
                            var err = new { status = "error", message = "User registration failed!" };
                            return(BadRequest(err));
                        }
                    }
                    else
                    {
                        //User Already exsist
                        var err = new { status = "error", message = "User already exsist!" };
                        return(BadRequest(err));
                    }
                }
                catch (Exception ex)
                {
                    var err = new { status = "error", message = ex.Message };
                    return(BadRequest(err));
                }
            }
            else
            {
                var err = new { status = "error", message = "Invalid details" };
                return(BadRequest(err));
            }
        }
Beispiel #12
0
        public async Task <IActionResult> EditUser(string userName, User user)
        {
            if (await UserMgr.FindByNameAsync(userName) != null)
            {
                var results = await UserMgr.UpdateAsync(user);

                if (results.Succeeded)
                {
                    return(Ok($"User with user Id of {userName} has been updated."));
                }

                return(NotFound($"User with user Name of {userName} does not exist"));
            }

            return(NotFound($"User with user Name of {userName} does not exist"));
        }
Beispiel #13
0
        public async Task <IActionResult> checkAuthorization()
        {
            if (User?.Identity.IsAuthenticated == true)
            {
                var username = User?.Identity.Name;
                Console.WriteLine(username);
                var userToVerify = await UserMgr.FindByNameAsync(username);

                var userToSend = new UserModel();
                userToSend.FirstName = userToVerify.FirstName;
                userToSend.LastName  = userToVerify.LastName;
                userToSend.UserName  = userToVerify.NormalizedEmail.ToLower();
                return(Ok(userToSend));
            }
            return(Ok(false));
        }
Beispiel #14
0
        public async Task <IActionResult> getinfoAsync()
        {
            string  username = User.Claims.First(c => c.Type == "UserName").Value;
            AppUser user     = await UserMgr.FindByNameAsync(username);

            var returnuser = new
            {
                Username         = user.UserName,
                Email            = user.Email,
                Fullname         = user.UserName,
                Address          = user.Address,
                Type             = user.Type,
                EmailConfirmed   = user.EmailConfirmed,
                PhoneNumber      = user.PhoneNumber,
                TwoFactorEnabled = user.TwoFactorEnabled
            };

            return(Ok(returnuser));
        }
        public async Task <ActionResult <IdentityResult> > post([FromBody] JsonElement body)
        {
            Dictionary <string, string> json = JsonConvert.DeserializeObject <Dictionary <string, string> >(body.ToString());
            string password = json["password"];

            ApplicationUser appUser = new ApplicationUser(json["email"], json["firstName"], json["lastName"], json["email"]);
            IdentityResult  result;

            ApplicationUser user = await UserMgr.FindByNameAsync(appUser.Email);

            if (user == null)
            {
                //appUser.NormalizedUserName = appUser.Email;
                result = await UserMgr.CreateAsync(appUser, password);


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

                user = await UserMgr.FindByNameAsync(appUser.Email);

                //update
                user.UserName = json["userName"];
                context.UpdateUser(appUser);

                //emaim, password, mantiente login, bloqueo por x fallos
                await SignInMgr.PasswordSignInAsync(appUser.Email, password, true, false);

                return(result);
            }
            else
            {
                IdentityError error = new IdentityError();
                error.Code        = "10";
                error.Description = "Usuario existente";
                return(IdentityResult.Failed(error));
            }
        }
Beispiel #16
0
        public async Task<IActionResult> Register(User newUser)
        {
            try
            {
                ViewBag.Message = "User already registered";
                AppUser user = await UserMgr.FindByNameAsync(newUser.userName);
                if (user == null)
                {                    
                    user = new AppUser();
                    user.UserName = newUser.userName;
                    user.Email = newUser.eMail;
                    user.FirstName = newUser.firstName;
                    user.LastName = newUser.lastName;

                    IdentityResult result;

                    result = await UserMgr.CreateAsync(user, newUser.password);
                    
                    if(result.Succeeded)
                    {
                        ViewBag.Message = "User created!";
                    }
                    else
                    {
                        ViewBag.Message = result.Errors;
                    }


                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
            }

            return View();
        }
Beispiel #17
0
        public async Task <IActionResult> Update(string field, string value)
        {
            Console.WriteLine(field);
            Console.WriteLine(value);
            var username = User?.Identity.Name;
            var user     = await UserMgr.FindByNameAsync(username);

            if (field == "firstName")
            {
                user.FirstName = value;
            }
            else if (field == "lastName")
            {
                user.LastName = value;
            }
            else if (field == "newPassword")
            {
                await UserMgr.RemovePasswordAsync(user);

                await UserMgr.AddPasswordAsync(user, value);
            }
            else if (field == "newEmail")
            {
                Console.WriteLine(RegexUtilities.IsValidEmail(value));
                if (RegexUtilities.IsValidEmail(value))
                {
                    var checkEmail = await UserMgr.FindByEmailAsync(value);

                    if (checkEmail == null)
                    {
                        await UserMgr.SetEmailAsync(user, value);

                        var newUser = await UserMgr.FindByEmailAsync(value);

                        await SignInMgr.SignOutAsync();

                        await SignInMgr.SignInAsync(newUser, true);

                        user = newUser;
                    }
                    else
                    {
                        return(Ok("Email exists"));
                    }
                }
                else
                {
                    return(Ok("Email invalid"));
                }
            }

            IdentityResult result = await UserMgr.UpdateAsync(user);

            Console.WriteLine(result.Succeeded + "<----");
            if (result.Succeeded)
            {
                var userToSend = new UserModel();
                userToSend.FirstName = user.FirstName;
                userToSend.LastName  = user.LastName;
                userToSend.UserName  = user.NormalizedEmail;
                return(Ok(userToSend));
            }
            return(Ok("Accout update failed"));
        }