예제 #1
0
        private async Task <IActionResult> RenderPage()
        {
            LoggedUser = await userManager.GetUserAsync(User);

            if (LoggedUser == null)
            {
                return(RedirectToPage("../Index"));
            }

            var roles = await userManager.GetRolesAsync(LoggedUser);

            LoggedUserRole = roles[0];

            /*User private details.*/
            UserData = memberData.GetMemberById(LoggedUser.Id);

            /*If there was an account created before private details database was delivered,
             *  temporarly create an instance of Member class.*/
            if (UserData == null)
            {
                UserData = new Member()
                {
                    IdentityId = LoggedUser.Id,
                };
                memberData.Add(UserData);
                memberData.Commit();
            }

            return(Page());
        }
예제 #2
0
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = new IdentityUser {
                UserName = userInput.Email, Email = userInput.Email
            };
            var result = await userManager.CreateAsync(user, userInput.Password);

            if (result.Succeeded)
            {
                //Token generation for email confirmation
                var token = await userManager.GenerateEmailConfirmationTokenAsync(user);

                //URL generating
                var confirmationLink = Url.Page("/Account/ConfirmEmail", null, new { userId = user.Id, token = token }, Request.Scheme);
                //Generating mail data
                var message = new Message(new string[] { user.Email }, "Confirm your email address.", confirmationLink);
                //Send autorization link
                emailSender.SendAutorizationEmail(message);

                //New member creation <- for user personal details
                var member = new Member()
                {
                    IdentityId = user.Id
                };
                memberData.Add(member);
                memberData.Commit();

                //Create customer role if not existing in DB
                if (!await roleManager.RoleExistsAsync("Customer"))
                {
                    var identityRole = new IdentityRole("Customer");
                    await roleManager.CreateAsync(identityRole);
                }
                //Add customer user to newly created user
                await userManager.AddToRoleAsync(user, "Customer");

                Message = "Confirm your email address to login.";

                return(RedirectToPage("./Login"));
            }

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

            return(Page());
        }