Пример #1
0
        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                // Validate the user password
                var             manager = new UserManager();
                ApplicationUser user    = manager.Find(UserName.Text, Password.Text);


                if (user != null)
                {
                    //Migrate shoppint cart. When the shopping cart is migrated,
                    //the GUID used to identify the anonymous shopping cart is
                    //replaced with the user name.

                    //create new instance of shopping cart
                    ShoppingCartEngine usersShoppingCart = new ShoppingCartEngine();

                    //retrieve GUID
                    String cartId = usersShoppingCart.GetCartId();

                    //pass cartId and UserName to Migrate cart method
                    usersShoppingCart.MigrateCart(cartId, UserName.Text);

                    IdentityHelper.SignIn(manager, user, RememberMe.Checked);
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else
                {
                    FailureText.Text     = "Invalid username or password.";
                    ErrorMessage.Visible = true;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Update the cart items
        /// </summary>
        /// <returns></returns>
        public List <CartItem> UpdateCartItems()
        {
            using (ShoppingCartEngine usersShoppingCart = new ShoppingCartEngine())
            {
                String cartId = usersShoppingCart.GetCartId();

                ShoppingCartEngine.ShoppingCartUpdates[] cartUpdates = new ShoppingCartEngine.ShoppingCartUpdates[CartList.Rows.Count];
                for (int i = 0; i < CartList.Rows.Count; i++)
                {
                    IOrderedDictionary rowValues = new OrderedDictionary();
                    rowValues = GetValues(CartList.Rows[i]);
                    cartUpdates[i].ProductId = Convert.ToInt32(rowValues["ProductID"]);

                    CheckBox cbRemove = new CheckBox();
                    cbRemove = (CheckBox)CartList.Rows[i].FindControl("Remove");
                    cartUpdates[i].RemoveItem = cbRemove.Checked;

                    TextBox quantityTextBox = new TextBox();
                    quantityTextBox = (TextBox)CartList.Rows[i].FindControl("PurchaseQuantity");
                    cartUpdates[i].PurchaseQuantity = Convert.ToInt16(quantityTextBox.Text.ToString());
                }
                usersShoppingCart.UpdateShoppingCartDatabase(cartId, cartUpdates.ToList());
                CartList.DataBind();
                lblTotal.Text = String.Format("{0:c}", usersShoppingCart.GetTotal());
                return(usersShoppingCart.GetCartItems());
            }
        }
Пример #3
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            // var userStore = new UserStore<IdentityUser>();
            var userManager = new UserManager();
            var user        = new ApplicationUser()
            {
                UserName = UserName.Text
            };

            try
            {
                IdentityResult result = userManager.Create(user, Password.Text);

                RoleManager <IdentityRole> roleManager = new RoleManager <IdentityRole>(
                    new RoleStore <IdentityRole>(new ApplicationDbContext()));

                if (result.Succeeded)
                {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity          = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    //create a user role
                    CreateRole("Customer");

                    var store = new UserStore <ApplicationUser>(new ApplicationDbContext());
                    store.AutoSaveChanges = false;
                    var currentUserId = User.Identity.GetUserId();
                    var manager       = new UserManager <ApplicationUser>(store);
                    var currentUser   = manager.FindById(User.Identity.GetUserId());


                    //get current user
                    //var currentUser = userManager.FindByName(user.UserName);

                    StatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName);
                    IdentityHelper.SignIn(userManager, user, isPersistent: false);

                    //migrate shopping cart
                    using (ShoppingCartEngine usersShoppingCart = new ShoppingCartEngine())
                    {
                        //get the GUID and assign to cardId
                        String cartId = usersShoppingCart.GetCartId();

                        //pass cartId and User id to migrateCart method
                        usersShoppingCart.MigrateCart(cartId, user.Id);
                    }

                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);


                    //Add user to role
                    var roleresult = userManager.AddToRole(currentUser.Id, "Customer");

                    if (!roleManager.RoleExists("Customer"))
                    {
                        Session["Error"] = "Role does not exist";

                        Response.Redirect("/UserPages/ErrorPage.aspx");
                    }

                    if (!System.Web.HttpContext.Current.User.IsInRole("Customer"))
                    {
                        Session["Error"] = "user is not in role";

                        Response.Redirect("/UserPages/ErrorPage.aspx");
                    }

                    authenticationManager.SignIn(new AuthenticationProperties()
                    {
                    }, userIdentity);
                    Response.Redirect("~/Login.aspx");
                }
                else
                {
                    ErrorMessage.Text = result.Errors.FirstOrDefault();
                }
            }
            catch (EntityCommandExecutionException ex)
            {
                Session["Error"] = ex;
                Response.Redirect("/UserPages/ErrorPage.aspx");
            }
        }