Example #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new PMSUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var defaultRole = "User";
                    await UserManager.AddToRoleAsync(user.Id, defaultRole);

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
 public PMSUser ValidateGoogleUser(string username)
 {
     try
     {
         SqlStoredProcedure sp = new SqlStoredProcedure("[dbo].[usp_GetOAuthDetails]", ConfigManager.GetNewSqlConnection);
         sp.AddParameterWithValue("@UserName", SqlDbType.VarChar, 256, ParameterDirection.Input, username);
         var ds = sp.ExecuteDataSet();
         if (ds.Tables.Count > 0)
         {
             PMSUser user = new PMSUser()
             {
                 UserId       = ds.Tables[0].Rows[0]["UserId"].ToString(),
                 UserName     = ds.Tables[0].Rows[0]["UserName"].ToString(),
                 Email        = ds.Tables[0].Rows[0]["Email"].ToString(),
                 UserFullName = ds.Tables[0].Rows[0]["UserFullName"].ToString()
             };
             return(user);
         }
         else
         {
             return(null);
         }
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
        public ActionResult UserProfile()
        {
            PMSUser currUser = UserManager.FindById(User.Identity.GetUserId());

            RegisterModel registerModel = new RegisterModel
            {
                FirstName = currUser.FirstName,
                LastName  = currUser.LastName,
                Email     = currUser.Email,
                Phone     = currUser.PhoneNumber,
                Image     = currUser.ImageUrl
            };



            return(View("Register", registerModel));
        }
        public async Task <ActionResult> Register(RegisterModel model, HttpPostedFileBase userImage)
        {
            if (ModelState.IsValid)
            {
                if (userImage != null)
                {
                    string photoUserEx         = Path.GetExtension(userImage.FileName);
                    string photoUserCustomName = model.FirstName + "-" + DateTime.Today.ToString("MM-dd-yy") + "-" + System.Guid.NewGuid().ToString("N") + photoUserEx;
                    string photoUserToPath     = Path.Combine(Server.MapPath("~/Content/Uploads/Profile"), photoUserCustomName);
                    string photoPath           = "~/" + photoUserToPath.Substring(photoUserToPath.IndexOf("Content")).Replace('\\', '/');
                    // file is uploaded
                    userImage.SaveAs(photoUserToPath);
                    model.Image = photoPath;
                }

                PMSUser user = new PMSUser()
                {
                    FirstName            = model.FirstName,
                    LastName             = model.LastName,
                    ImageUrl             = model.Image,
                    RegistrationDateTime = DateTime.Now,
                    UserName             = model.Email,
                    Email       = model.Email,
                    PhoneNumber = model.Phone
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("Index", "Home"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error);
                }
            }


            return(View(model));
        }
Example #5
0
        //public OperationResult<UserModel> Authenticate(LoginModel model)
        //{
        //    if (string.IsNullOrEmpty(model.Email) || string.IsNullOrEmpty(model.Password))
        //    {
        //      return  new OperationResult<UserModel>("Email and Password must be filled", IssueSeverity.Error);
        //    }

        //    using (var db = new PMSContext())
        //    {
        //        var user = db.Users.FirstOrDefault(u=>u.Email.Equals(model.Email, StringComparison.OrdinalIgnoreCase));
        //        if (user == null || user.PasswordHash != GetPasswordHash(model.Password))
        //        {
        //         return   new OperationResult<UserModel>("User not found", IssueSeverity.Error);
        //        }

        //        return new OperationResult<UserModel>(new UserModel() {
        //            Email = user.Email,

        //        });
        //    }
        //}

        //private const string HASH_SECRET_KEY = "15042CEA-9F65-47C2-B7B6-0DAD6ED4CB8D";
        //private static string GetPasswordHash(string password)
        //{
        //    using (var hash = SHA256.Create())
        //    {
        //        return string.Join("", hash
        //          .ComputeHash(Encoding.ASCII.GetBytes($"{password}-{HASH_SECRET_KEY}"))
        //          .Select(item => item.ToString("x2")));
        //    }
        //}

        public async Task <OperationResult <IdentityResult> > Register(RegisterModel model)
        {
            User = new PMSUser()
            {
                FirstName            = model.FirstName,
                LastName             = model.LastName,
                ImageUrl             = model.Image,
                RegistrationDateTime = DateTime.Now,
                UserName             = model.Email,
                Email       = model.Email,
                PhoneNumber = model.Phone
            };

            UserStore <PMSUser>   userStore = new UserStore <PMSUser>(new PMSContext());
            UserManager <PMSUser> manager   = new UserManager <PMSUser>(userStore);

            var result = await manager.CreateAsync(User, model.Password);

            return(new OperationResult <IdentityResult>(result));
        }
Example #6
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new PMSUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Example #7
0
        public async Task <JsonResult> Action(UserActionModel model)
        {
            JsonResult json = new JsonResult();

            IdentityResult result = null;

            if (!string.IsNullOrEmpty(model.ID)) //we are trying to edit a record
            {
                var user = await UserManager.FindByIdAsync(model.ID);

                user.FullName = model.FullName;
                user.Email    = model.Email;
                user.UserName = model.UserName;
                user.Country  = model.Country;
                user.City     = model.City;
                user.Address  = model.Address;

                result = await UserManager.UpdateAsync(user);
            }
            else //we are trying to create a record
            {
                var user = new PMSUser();

                user.FullName = model.FullName;
                user.Email    = model.Email;
                user.UserName = model.UserName;
                user.Country  = model.Country;
                user.City     = model.City;
                user.Address  = model.Address;

                result = await UserManager.CreateAsync(user);
            }

            json.Data = new { Success = result.Succeeded, Message = string.Join(", ", result.Errors) };

            return(json);
        }