コード例 #1
0
        public Task <MyUser> FindByIdAsync(string userId)
        {
            if (string.IsNullOrWhiteSpace(userId))
            {
                return(Task.FromResult(default(MyUser)));
            }

            var id          = int.Parse(userId);
            var userRequest = _userService.GetUser(id);

            return(Task.FromResult(userRequest != null ? MappUser(userRequest) : default(MyUser)));
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new MyUser
                {
                    UserName      = model.Email,
                    Email         = model.Email,
                    Password      = model.Password,
                    Firstname     = model.Firstname,
                    Lastname      = model.Lastname,
                    Active        = true,
                    EmailVerified = false,
                    Phone         = model.Phone,
                    PhoneVerified = false,
                    SecurityStamp = Guid.NewGuid().ToString()
                };

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

                if (result.Succeeded)
                {
                    try
                    {
                        // var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

                        var userRecord = _userService.GetUser(model.Email);
                        var code       = await UserManager.GenerateEmailConfirmationTokenAsync(userRecord.Id);

                        if (Request.Url != null)
                        {
                            var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userRecord.Id, code }, protocol: Request.Url.Scheme);
                            var message     = new MyIdentityMessage
                            {
                                Destination = model.Email,
                                Body        = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>",
                                Subject     = "Confirm your account"
                            };
                            await UserManager.EmailService.SendAsync(message);
                        }
                        AddViewMessage(StandardMessages.CustomMessageInfo, "Registeration successful. We have sent you a confirmation email, please confirm your email address to complete your registeration.");
                    }
                    catch (Exception ex)
                    {
                        AddViewMessage(StandardMessages.CustomMessageError, "Registeration successful but there was an error sending confirmation email.");
                    }

                    try
                    {
                        await SignInManager.PasswordSignInAsync(model.Email, model.Password, true, shouldLockout : false);
                    }
                    catch (Exception ex)
                    {
                        var error = ex.Message;
                        // ignored
                    }

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

            return(View(model));
        }