예제 #1
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            bool error = false;

            string userName = UserName.Text;

            if (userName == "" || userName.Length < 3)
            {
                ErrorSuccessNotifier.AddErrorMessage("The username must be at least 3 characters");
                error = true;
            }

            string password = Password.Text;

            if (password == "" || password.Length < 6)
            {
                ErrorSuccessNotifier.AddErrorMessage("The password must be at least 6 characters");
                error = true;
            }

            string confirmPassword = ConfirmPassword.Text;

            if (confirmPassword != password)
            {
                ErrorSuccessNotifier.AddErrorMessage("The passwords does not match");
                error = true;
            }

            if (error)
            {
                return;
            }

            var  manager = new AuthenticationIdentityManager(new IdentityStore());
            User u       = new User(userName)
            {
                UserName = userName
            };
            IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);

            if (result.Success)
            {
                ErrorSuccessNotifier.AddSuccessMessage("Successfully registered");
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName = TextBoxUserName.Text;
            var    manager  = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
            User   u        = new User(userName);

            IdentityResult result = manager.Users.CreateLocalUser(u, TextBoxPassword.Text);

            if (result.Success)
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
 protected void LogIn(object sender, EventArgs e)
 {
     if (IsValid)
     {
         // Validate the user password
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
         IdentityResult         result  = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
         if (result.Success)
         {
             OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
         }
         else
         {
             FailureText.Text     = result.Errors.FirstOrDefault();
             ErrorMessage.Visible = true;
         }
     }
 }
예제 #4
0
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }
            var user = new User(userName.Text);
            IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication;
            IdentityResult         result  = manager.CreateAndSignInExternalUser(Context.GetOwinContext().Authentication, user);

            if (result.Success)
            {
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                AddErrors(result);
                return;
            }
        }
예제 #5
0
        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                bool   error    = false;
                string username = this.UserName.Text;
                if (username == "")
                {
                    ErrorSuccessNotifier.AddErrorMessage("The username field is required");
                    error = true;
                }

                string password = this.Password.Text;
                if (password == "")
                {
                    ErrorSuccessNotifier.AddErrorMessage("The password field is required");
                    error = true;
                }

                if (error)
                {
                    return;
                }

                // Validate the user password
                IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication;
                IdentityResult         result  = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
                if (result.Success)
                {
                    OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else
                {
                    FailureText.Text     = result.Errors.FirstOrDefault();
                    ErrorMessage.Visible = true;
                }
            }
        }
예제 #6
0
 protected void Page_Load()
 {
     // Process the result from an auth provider in the request
     ProviderName = OpenAuthProviders.GetProviderNameFromRequest(Request);
     if (String.IsNullOrEmpty(ProviderName))
     {
         Response.Redirect("~/Account/Login");
     }
     if (!IsPostBack)
     {
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication;
         var            auth            = Context.GetOwinContext().Authentication;
         ClaimsIdentity id     = manager.GetExternalIdentity(auth);
         IdentityResult result = manager.SignInExternalIdentity(auth, id);
         if (result.Success)
         {
             OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
         }
         else if (User.Identity.IsAuthenticated)
         {
             result = manager.LinkExternalIdentity(id, User.Identity.GetUserId());
             if (result.Success)
             {
                 OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
             }
             else
             {
                 AddErrors(result);
                 return;
             }
         }
         else
         {
             userName.Text = id.Name;
         }
     }
 }