private async Task SignInAsync(CodeLib.ApplicationUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties {
         IsPersistent = isPersistent
     }, await user.GenerateUserIdentityAsync(UserManager));
 }
Exemplo n.º 2
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 CodeLib.ApplicationUser {
                    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));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> UserMgmt(UserViewModel userModel)
        {
            if (ModelState.IsValid || (userModel.UserId > 0 && string.IsNullOrWhiteSpace(userModel.Password)))
            {
                ApplicationUser identityUser     = null;
                IdentityResult  result           = null;
                int?            userRoleId       = null;
                bool            sendWelcomeEmail = (userModel.UserId <= 0);

                if (userModel.UserId <= 0)
                {
                    // Create the AspNet Identity user
                    identityUser = new CodeLib.ApplicationUser {
                        UserName = userModel.Email, Email = userModel.Email
                    };
                    result = await UserManager.CreateAsync(identityUser, userModel.Password);

                    if (result.Succeeded)
                    {
                        userModel.UserId = identityUser.Id;
                        userRoleId       = (int)RoleIdEnum.AppUser;
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }

                if (result == null || result.Succeeded)
                {
                    #region Create AppUser Record
                    try
                    {
                        AppUser appUser        = null;
                        int?    loggedInUserId = (Request.IsAuthenticated) ? User.Identity.GetUserId <int>() : new int();

                        using (var dbContext = new CodeLib.Models.Entities())
                        {
                            int userId = dbContext.AppUser_InsertUpdate(userModel.UserId, userModel.StatusId,
                                                                        userRoleId, userModel.FirstName, userModel.LastName, userModel.Email, loggedInUserId).FirstOrDefault() ?? -1;

                            if (userId == userModel.UserId)
                            {
                                appUser = new AppUser
                                {
                                    UserId       = userId,
                                    StatusId     = userModel.StatusId,
                                    FirstName    = userModel.FirstName,
                                    LastName     = userModel.LastName,
                                    Email        = userModel.Email,
                                    IdentityUser = identityUser
                                };
                            }
                            else
                            {
                                CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The DB failed to save AppUser record for " + userModel.FirstName + " " + userModel.LastName, "AdminController.cs >> UserMgmt() >> AppUser_InsertUpdate()", identityUser.Id.ToString());
                            }
                        }

                        if (appUser != null)
                        {
                            if (sendWelcomeEmail)
                            {
                                // Send an email with this link
                                string code = await UserManager.GenerateEmailConfirmationTokenAsync(appUser.UserId);

                                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = appUser.UserId, code = code }, protocol: Request.Url.Scheme);

                                if (!CodeLib.Email.EmailTemplate.SendRegistrationEmail(appUser, callbackUrl))
                                {
                                    CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The registration confirmation email failed to send to " + appUser.Email, "AdminController.cs >> UserMgmt()", appUser.UserId.ToString());
                                }
                            }

                            return(RedirectToAction("UsersMgmt"));
                        }
                        else
                        {
                            AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        }
                    }
                    catch (Exception ex)
                    {
                        AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, SiteUtils.GetPageName(), null, ex.Message, ex.StackTrace, userModel.Email);
                    }
                    #endregion
                }
            }

            var statusList = await CommonDAL.GetLookupList(LookupTypeIdEnum.AppUserStatus);

            userModel.StatusList = statusList.Select(status => new SelectListItem {
                Text = status.Descr, Value = status.LookupId.ToString()
            }).ToList();

            return(View(userModel));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Register(UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new CodeLib.ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    #region Create AppUser Record
                    try
                    {
                        AppUser appUser = null;

                        using (var dbContext = new CodeLib.Models.Entities())
                        {
                            int userId = dbContext.AppUser_InsertUpdate(user.Id, (int)DatabaseIdEnum.UserStatus_Active,
                                                                        (int)RoleIdEnum.AppUser, model.FirstName, model.LastName, null, user.Id).FirstOrDefault() ?? -1;

                            if (userId == user.Id)
                            {
                                appUser = new AppUser
                                {
                                    UserId       = user.Id,
                                    FirstName    = model.FirstName,
                                    LastName     = model.LastName,
                                    Email        = model.Email,
                                    IdentityUser = user
                                };
                            }
                            else
                            {
                                CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The DB failed to create AppUser record for " + model.FirstName + " " + model.LastName, "AccountController.cs >> Register() >> AppUser_InsertUpdate()", user.Id.ToString());
                            }
                        }

                        if (appUser != null && appUser.IdentityUser != null)
                        {
                            // For more information on how to enable account confirmation and password reset please visit http://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>");

                            if (!EmailTemplate.SendRegistrationEmail(appUser, callbackUrl))
                            {
                                CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, "The registration confirmation email failed to send to " + user.Email, "AccountController.cs >> Register()", user.Id.ToString());
                            }

                            return(RedirectToAction("Login", "Account"));
                        }
                        else
                        {
                            AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        }
                    }
                    catch (Exception ex)
                    {
                        AddErrors(new IdentityResult(new string[] { "Oops! An error has occurred. " + CommonObjects.ERROR_MSG_SUPPORT }));
                        CommonDAL.InsertExceptionLog(DatabaseIdEnum.LogType_SiteException, SiteUtils.GetPageName(), null, ex.Message, ex.StackTrace, user.Email);
                    }
                    #endregion
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }