Esempio n. 1
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        UserStore <IdentityUser> userStore = new UserStore <IdentityUser>();

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

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

        IdentityUser user = new IdentityUser();

        user.UserName = txtUserName.Text;

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

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

                    UserInfoModel model = new UserInfoModel();
                    model.InsertInformations(info);

                    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();
                throw;
            }
        }
        else
        {
            litStatus.Text = "Passwords must match!";
        }
    }