コード例 #1
0
        public async Task <ActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                SmsServiceUser user = new SmsServiceUser
                {
                    UserName = model.Nickname,
                    Email    = model.Email
                };

                IdentityResult result = await Usermanager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var role = RoleManager.FindByName("admin");
                    Usermanager.AddToRole(user.Id, role.Name);
                    return(RedirectToAction("Index", "Home"));
                }

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

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> SingUP(SignUp Model)
        {
            if (ModelState.IsValid)
            {
                AppUser user = new AppUser();
                user.UserName    = Model.UserName;
                user.PhoneNumber = Model.PhoneNumber;
                user.Email       = Model.Email;

                IdentityResult identityResult = await Usermanager.CreateAsync(user, Model.Password);

                if (identityResult.Succeeded)
                {
                    return(RedirectToAction("Login"));
                }
                else
                {
                    AddErrors(identityResult);
                }
            }
            return(View());
        }
コード例 #3
0
        public async Task <IActionResult> AddEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)

            {
                var user = new ApplicationUser
                {
                    FullName     = model.FullName,
                    UserName     = model.Email,
                    Email        = model.Email,
                    City         = model.cityId,
                    PhoneNumber  = model.PhoneNumber,
                    StreetAdress = model.StreetAdress,
                    store_id     = model.store_id,
                    isactive     = true,
                    Photopath    = util.ProcessPhotoproperty(model.Photo)
                };

                var LoginUser = await Usermanager.GetUserAsync(User);

                user.addedBy = LoginUser.Id;


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

                if (result.Succeeded)
                {
                    foreach (var role in model.Roles.Where(x => x.isSelected))
                    {
                        if (User.IsInRole("Admin") && role.RoleName == "Admin" && role.RoleName == "Super Admin")
                        {
                            await Usermanager.DeleteAsync(user);

                            return(Forbid());
                        }
                    }


                    var roles = await Usermanager.AddToRolesAsync(user, model.Roles.Where(x => x.isSelected).Select(y => y.RoleName));

                    if (!roles.Succeeded)
                    {
                        ModelState.AddModelError("", "Cannot add selected roles to user! Try Again");
                        await Usermanager.DeleteAsync(user);

                        return(View(model));
                    }
                    // Add all the claims that are selected on the UI
                    var claims = await Usermanager.AddClaimsAsync(user,
                                                                  model.Claims.Select(c => new Claim(c.ClaimType, c.isSelected ? "true" : "false")));

                    if (!claims.Succeeded)
                    {
                        ModelState.AddModelError("", "Cannot add selected claims to user!  Edit User and Insert Claims from there");
                        return(View(model));
                    }
                    var token = await Usermanager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);

                    string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/Email_Confirmation.cshtml", confirmationLink);

                    await _emailSender.SendEmailAsync(user.Email, "Email Confirmation", str);

                    //util.sendemail(user.Email, "Email Confirmation", $"<h2>Here is the Confirmation Link</h2></br> <a href={confirmationLink}>{confirmationLink}</a>");
                    if (Signinmanager.IsSignedIn(User) && (User.IsInRole("Admin") || User.IsInRole("Super Admin")))
                    {
                        return(RedirectToAction("ListUsers", "Administration"));
                    }
                    ViewBag.PageTitle = "Email Confirmation";
                    ViewBag.Title     = "Registration successful";
                    ViewBag.Message   = "Before you can Login, please confirm your " +
                                        "email, by clicking on the confirmation link we have emailed you";
                    return(View("EmailConfirmation"));

                    // await Signinmanager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("Index", "Home");
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                ViewBag.Stores = util.GetAllStores();
                ViewBag.cities = util.getCities();
                return(View(model));
            }
            ViewBag.cities = util.getCities();
            ViewBag.Stores = util.GetAllStores();
            return(View(model));
        }
コード例 #4
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var LoginUser = await Usermanager.GetUserAsync(User);

                var user = new ApplicationUser
                {
                    FullName     = model.FullName,
                    UserName     = model.Email,
                    Email        = model.Email,
                    City         = model.cityId,
                    PhoneNumber  = model.PhoneNumber,
                    StreetAdress = model.StreetAdress,
                    Photopath    = util.ProcessPhotoproperty(model.Photo),
                    isactive     = true
                };
                if (Signinmanager.IsSignedIn(User) && User.IsInRole("Employee"))
                {
                    if (!User.HasClaim(claim => claim.Type == "Create User" && claim.Value == "true"))
                    {
                        return(Forbid());
                    }
                    user.store_id = LoginUser.store_id;
                    user.addedBy  = LoginUser.Id;
                }

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

                var roles = await Usermanager.AddToRoleAsync(user, "User");


                if (result.Succeeded)
                {
                    var token = await Usermanager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);

                    string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/Email_Confirmation.cshtml", confirmationLink);

                    await _emailSender.SendEmailAsync(user.Email, "Email Confirmation", str);

                    ViewBag.PageTitle = "Email Confirmation";
                    ViewBag.Title     = "Registration successful";
                    ViewBag.Message   = "Before you can Login, please confirm your " +
                                        "email, by clicking on the confirmation link we have emailed you";
                    return(View("EmailConfirmation"));

                    // await Signinmanager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("Index", "Home");
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                ViewBag.Stores = util.GetAllStores();
                ViewBag.cities = util.getCities();
                return(View(model));
            }
            ViewBag.Stores = util.GetAllStores();
            ViewBag.cities = util.getCities();
            return(View(model));
        }
コード例 #5
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            LoginViewModel loginViewModel = new LoginViewModel
            {
                ReturnUrl      = returnUrl,
                ExternalLogins =
                    (await Signinmanager.GetExternalAuthenticationSchemesAsync()).ToList()
            };

            if (remoteError != null)
            {
                ModelState
                .AddModelError(string.Empty, $"Error from external provider: {remoteError}");

                return(View("Login", loginViewModel));
            }

            // Get the login information about the user from the external login provider
            var info = await Signinmanager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ModelState
                .AddModelError(string.Empty, "Error loading external login information.");

                return(View("Login", loginViewModel));
            }

            // If the user already has a login (i.e if there is a record in AspNetUserLogins
            // table) then sign-in the user with this external login provider
            var signInResult = await Signinmanager.ExternalLoginSignInAsync(info.LoginProvider,
                                                                            info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (signInResult.Succeeded)
            {
                return(LocalRedirect(returnUrl));
            }
            // If there is no record in AspNetUserLogins table, the user may not have
            // a local account
            else
            {
                // Get the email claim value
                var email = info.Principal.FindFirstValue(ClaimTypes.Email);

                if (email != null)
                {
                    // Create a new user without password if we do not have a user already
                    var user = await Usermanager.FindByEmailAsync(email);

                    if (user == null)
                    {
                        user = new ApplicationUser
                        {
                            UserName = info.Principal.FindFirstValue(ClaimTypes.Email),
                            Email    = info.Principal.FindFirstValue(ClaimTypes.Email)
                        };

                        await Usermanager.CreateAsync(user);

                        await Usermanager.AddToRoleAsync(user, "User");
                    }

                    // Add a login (i.e insert a row for the user in AspNetUserLogins table)
                    await Usermanager.AddLoginAsync(user, info);

                    await Signinmanager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }

                // If we cannot find the user email we cannot continue
                ViewBag.ErrorTitle   = $"Email claim not received from: {info.LoginProvider}";
                ViewBag.ErrorMessage = "Please contact support on " + Configuration["Email"];

                return(View("Error"));
            }
        }