예제 #1
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        var userStore = new UserStore <IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            System.Configuration.ConfigurationManager.
            ConnectionStrings["StoreDBConnectionString"].ConnectionString;

        var manager = new UserManager <IdentityUser>(userStore);

        var user = new IdentityUser {
            UserName = txtUserName.Text
        };

        if (txtPassword.Text == txtConfirmPassword.Text)
        {
            try
            {
                IdentityResult result = manager.Create(user, txtPassword.Text);

                if (result.Succeeded)
                {
                    UserInfo userDetail = new UserInfo
                    {
                        Address    = txtAddress.Text,
                        FirstName  = txtFirstName.Text,
                        LastName   = txtLastName.Text,
                        GUID       = user.Id,
                        PostalCode = Convert.ToInt32(txtPostCode.Text)
                    };

                    UserInfoModel model = new UserInfoModel();
                    model.InsertUserInfo(userDetail);

                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;

                    var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                    authenticationManager.SignIn(new AuthenticationProperties(), userIdentity);
                    Response.Redirect("~/Index.aspx");
                }
                else
                {
                    litStatus.Text = result.Errors.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                litStatus.Text = ex.ToString();
            }
        }
        else
        {
            litStatus.Text = "Password must match";
        }
    }
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            UserStore <IdentityUser> userStore = new UserStore <IdentityUser>();

            //https://stackoverflow.com/questions/20183777/keyword-not-supported-metadata
            string connectionStringEF = System.Configuration.ConfigurationManager
                                        .ConnectionStrings["VegetableDBEntities"].ConnectionString;
            var efBuilder = new System.Data.Entity.Core.EntityClient
                            .EntityConnectionStringBuilder(connectionStringEF);
            string connectionStringOK = efBuilder.ProviderConnectionString;

            userStore.Context.Database.Connection.ConnectionString = connectionStringOK;

            var manager = new UserManager <IdentityUser>(userStore);

            var user = new IdentityUser();

            user.UserName = txtLogin.Text;

            if (txtPasword.Text == txtConfirmPassword.Text)
            {
                try {
                    var result = manager.Create(user, txtPasword.Text);
                    if (result.Succeeded)
                    {
                        var userInfo = new UserInfo {
                            FirstName = txtFirstName.Text,
                            LastName  = txtLastName.Text,
                            Address   = txtAddress.Text,
                            Phone     = txtPhone.Text,
                            Guid      = user.Id
                        };
                        var userInfoModel = new UserInfoModel();
                        userInfoModel.InsertUserInfo(userInfo);

                        var autentificationManager = HttpContext.Current.GetOwinContext().Authentication;
                        var userIdentity           = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                        autentificationManager
                        .SignIn(new Microsoft.Owin.Security.AuthenticationProperties(), userIdentity);
                        Response.Redirect("~/Index.aspx");
                    }
                    else
                    {
                        litStatus.Text = result.Errors.FirstOrDefault();
                    }
                }
                catch (Exception ex) {
                    litStatus.Text = ex.ToString();
                }
            }
            else
            {
                litStatus.Text = "Пароли не совпадают";
            }
        }