コード例 #1
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = new CodeItUpUser
            {
                UserName = model.Username,
                CoolCar  = model.CoolCar
            };

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

            if (result.Succeeded)
            {
                await this.userManager
                .AddClaimAsync(user, new Claim("CodeItUp.Car", model.CoolCar));

                await this.signInManager.SignInAsync(user, false);

                return(Redirect(model.ReturnUrl));
            }

            return(View());
        }
コード例 #2
0
        public async Task <IActionResult> Register(string username, string password)
        {
            var user = new CodeItUpUser
            {
                UserName = username,
                Email    = "*****@*****.**",
                CoolCar  = "BMW 640D"
            };

            var result = await this.userManager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                await this.userManager
                .AddClaimAsync(user, new Claim("CodeItUp.Car", user.CoolCar));

                var signInResult = await this.signInManager
                                   .PasswordSignInAsync(user, password, false, false);

                if (signInResult.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    // Show an error message.
                    throw new InvalidOperationException("Invalid login.");
                }
            }

            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public async Task <IActionResult> ExternalRegister(ExternalRegisterViewModel model)
        {
            var info = await this.signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(RedirectToAction("Login"));
            }

            var user = new CodeItUpUser
            {
                UserName = model.UserName
            };

            var result = await this.userManager.CreateAsync(user);

            if (!result.Succeeded)
            {
                return(View(model));
            }

            result = await this.userManager.AddLoginAsync(user, info);

            if (!result.Succeeded)
            {
                return(View(model));
            }

            await this.signInManager.SignInAsync(user, false);

            return(Redirect(model.ReturnUrl));
        }