0

I am currently working on a learning project where I used to use forms auth with an sql database. But today I have updated to using Indetity. The problem I have now is that my site stores more than user information, and I want that in the same database as the Identity data. When I used membership this was no problem, I just added a connectionstring and wrote an SQL statement. But now it seems I need to add something called DbContext? Maybe it's easier to look at my code to understand, This is my old code, used for old SQL database:

using (SqlConnection con = new SqlConnection(strCon))
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = "INSERT INTO Website (url, rating, categoryId, subCategoryId, description1, description2) values (@url, @rating, @categoryId, @subCategoryId, @desc1, @desc2)";
                        cmd.Parameters.AddWithValue("@url", url);
                        cmd.Parameters.AddWithValue("@rating", rating);
                        cmd.Parameters.AddWithValue("@categoryId", categoryId);
                        cmd.Parameters.AddWithValue("@desc1", desc1);
                        cmd.Parameters.AddWithValue("@desc2", desc2);
                        if (DPLSubCategory.Items != null)
                        {
                            Int32 subCategoryId = Convert.ToInt32(DPLSubCategory.SelectedItem.Value);
                            cmd.Parameters.AddWithValue("@subCategoryId", subCategoryId);
                        }

                        con.Open();

                        cmd.ExecuteNonQuery();
                    }
                    con.Close();
                }

Now say I add a "Website" table to my local Identity database. How can I do something similar to the code above? Right now I am using default connectionstring:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\WDBAPP.mdf;Initial Catalog=WDBAPP;Integrated Security=True" providerName="System.Data.SqlClient"/>

And I add members like this:

// Default UserStore constructor uses the default connection string named: DefaultConnection
            var userStore = new UserStore<IdentityUser>();
            var manager = new UserManager<IdentityUser>(userStore);
            var user = new IdentityUser() { UserName = UserName.Text };

            IdentityResult result = manager.Create(user, Password.Text);

            if (result.Succeeded)
            {
                var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
                Response.Redirect("~/Login.aspx");
            }

As you can see I have no need to point to my database since I use the default connectionstring.

So how do I change my connectionstring to, for example, "MyConString" and still have it work with the registration code? I want to be able to point to my database from my code so that I can add whatever I want.

Green_qaue
  • 3,561
  • 11
  • 47
  • 89
  • You already tagged your question with Entity Framework, I suggest you go read up on that and do a tutorial or two. That will probably help more than an answer to this question. – DavidG Feb 09 '15 at 16:31
  • possible duplicate of [How to add ASP.NET MVC5 Identity Authentication to existing database](http://stackoverflow.com/questions/25651342/how-to-add-asp-net-mvc5-identity-authentication-to-existing-database) – Win Feb 09 '15 at 17:05
  • All tutorials I find are for MVC. I work with web forms. Web forms do not have a applicationdbcontext class that I can find and modify, and I do not know how to create one. Correct me if Im wrong @Win – Green_qaue Feb 09 '15 at 17:25
  • Ive tried getting help at asp.net forum as well. I do not agree with you @Win that it's a duplicate, that question is not the same as mine, not really that close even. Maybe Im just missing something obvious, just chocked its so complicated to do something that the "outdated" version did with 1 line of code. – Green_qaue Feb 09 '15 at 17:33

1 Answers1

0

Web forms do not have a applicationdbcontext class that I can find and modify, and I do not know how to create one.

Default Web Form Template in VS 2013 has ApplicationDbContext.

If you are new to ASP.Net Identity, you want to use Default Template created by VS first before hand-rolling your own UserManager, because a lot of things can go wrong easily.

If you want to keep ASP.Net Identity inside your existing database, you need to rename it same as your existing connection string.

enter image description here

Win
  • 61,100
  • 13
  • 102
  • 181
  • Thanks for taking the time to answer. I didnt use the template, thats why I didnt find this. Is it fine to just create this manually in my empty project? Also, how do I use this to add custom data using c#? I think maybe I am too green in EF. Ill try to find a tutorial for web forms. Just want to add stuff to my Website table like above, but using EF. – Green_qaue Feb 09 '15 at 17:41
  • You can read a [free chapter](http://www.apress.com/files/extra/ASP_NET_Identity_Chapters.pdf) written by Adam Freeman. Even then ASP.Net Identity is a very deep topic. If you are new to ASP.Net Identity, you want to test with Default Template created by VS first before hand-rolling your own UserManager, because a lot of things can go wrong easily. – Win Feb 09 '15 at 17:47
  • Thanks. I think Ill just go back and use form auth for now. Don't have the time to learn a completely new topic atm. – Green_qaue Feb 09 '15 at 17:49
  • **FormAuthentication** might be a good choice if you do not need extra features. [Here](http://stackoverflow.com/a/28334010/296861) is an example. – Win Feb 09 '15 at 17:52