Exemplo n.º 1
0
        public override Task <SignInStatus> PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout)
        {
            if (ActiveDirectoryAuthentication.IsADAuthenticationEnabled())
            {
                if (ActiveDirectoryAuthentication.Authenticate(userName, password))
                {
                    return(base.PasswordSignInAsync(userName, "bazooka", isPersistent, shouldLockout));
                }
            }

            return(base.PasswordSignInAsync(userName, password, isPersistent, shouldLockout));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.UserName, Email = model.Email
                };

                if (db.Users.Count() == 0)
                {
                    user.Administrator = true;
                }

                IdentityResult result;

                if (ActiveDirectoryAuthentication.IsADAuthenticationEnabled())
                {
                    if (!ActiveDirectoryAuthentication.Authenticate(model.UserName, model.Password))
                    {
                        ModelState.AddModelError("", "username or password not valid");
                        return(View(model));
                    }
                    else
                    {
                        result = await UserManager.CreateAsync(user, "bazooka");
                    }
                }
                else
                {
                    result = await UserManager.CreateAsync(user, model.Password);
                }


                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // 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>");

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

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 3
0
        public static ApplicationUserManager Create(IdentityFactoryOptions <ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore <ApplicationUser>(context.Get <ApplicationDbContext>()));

            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator <ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail             = true
            };

            var provider = new DpapiDataProtectionProvider("Sample");

            manager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(provider.Create("EmailConfirmation"));

            if (ActiveDirectoryAuthentication.IsADAuthenticationEnabled())
            {
                // if AD authentication is enabled we defer to their password policy
                manager.PasswordValidator = new PasswordValidator
                {
                    RequiredLength          = 2,
                    RequireNonLetterOrDigit = false,
                    RequireDigit            = false,
                    RequireLowercase        = false,
                    RequireUppercase        = false,
                };
            }
            else
            {
                manager.PasswordValidator = new PasswordValidator
                {
                    RequiredLength          = 6,
                    RequireNonLetterOrDigit = true,
                    RequireDigit            = true,
                    RequireLowercase        = true,
                    RequireUppercase        = true,
                };
            }

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault          = true;
            manager.DefaultAccountLockoutTimeSpan        = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;


            return(manager);
        }