使用connectionstring将数据添加到Identity默认数据库
我目前正在使用一个学习项目,在那里我使用表单身份验证与sql数据库。但今天我已更新为使用Indetity。 我现在的问题是我的网站存储的不仅仅是用户信息,而且我希望与身份数据在同一个数据库中。 当我使用成员资格时,这没有问题,我只是添加了一个连接字符串并编写了一条SQL语句。但现在看来我需要添加一个名为DbContext的东西? 也许它更容易看我的代码来理解,这是我的旧代码,用于旧的SQL数据库:使用connectionstring将数据添加到Identity默认数据库
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();
}
现在说我添加了一个“网站”桌到我的本地身份数据库。我如何做类似于上面的代码的东西?现在我使用默认的ConnectionString:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\WDBAPP.mdf;Initial Catalog=WDBAPP;Integrated Security=True" providerName="System.Data.SqlClient"/>
我添加成员是这样的:
// 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");
}
正如你可以看到我有没必要点到我的数据库,因为我用的是默认的ConnectionString。
那么如何将连接字符串更改为例如“MyConString”并仍然可以使用注册码呢?我希望能够从我的代码中指向我的数据库,以便可以添加我想要的任何内容。
Web窗体没有applicationdbcontext类,我可以找到 并修改,我不知道如何创建一个。在VS 2013
默认Web表单模板ApplicationDbContext。
如果您是ASP.Net Identity的新用户,您希望首先使用由VS创建的默认模板,然后再手动滚动您自己的UserManager,因为很多事情很容易出错。
如果您想在现有数据库中保留ASP.Net Identity,则需要将其重命名为与现有连接字符串相同的名称。
感谢您花时间回答。我没有使用模板,这就是为什么我没有找到这个。在我的空项目中手动创建它可以吗?另外,我如何使用它来使用c#添加自定义数据?我想也许我太EF了。我试图找到一个Web窗体的教程。只是想像上面添加东西到我的网站表中,但使用EF。 – 2015-02-09 17:41:27
您可以阅读[免费章节](http://www.apress。com/files/extra/ASP_NET_Identity_Chapters.pdf)由Adam Freeman编写。即使如此,ASP.Net Identity也是一个非常深刻的话题。如果您是ASP.Net Identity的新手,您需要先使用由VS创建的默认模板进行测试,然后再手动滚动您自己的UserManager,因为很多事情很容易出错。 – Win 2015-02-09 17:47:51
谢谢。我认为我现在回去使用表单身份验证。没有时间学习一个全新的主题atm。 – 2015-02-09 17:49:54
您已经标记为实体框架你的问题,我建议你去上阅读起来,做一个或两个教程。这可能会帮助解决这个问题。 – DavidG 2015-02-09 16:31:46
[如何将ASP.NET MVC5身份验证添加到现有数据库]的可能重复(http://stackoverflow.com/questions/25651342/how-to-add-asp-net-mvc5-identity-authentication-to-existing -database) – Win 2015-02-09 17:05:01
我找到的所有教程都是针对MVC的。我使用网络表单工作。 Web表单没有可以找到和修改的applicationdbcontext类,我不知道如何创建它。纠正我,如果我错了@Win – 2015-02-09 17:25:41