更改在ASP.NET身份存储用户数据的数据库 [英] Change the database in which ASP.NET Identity stores user data
问题描述
我们已经创建了一个新的ASP.NET项目4.5.1如下:
- 的Visual Studio 2013
- 新项目
- 的Visual C#
- 网站
- ASP.NET Web应用程序
- 的Web API
- 更改身份验证
- 单个用户帐户
- 好>好
在解决方案资源管理> App_Start> Startup.Auth.cs文件中,有以下code它配置ASP.NET恒等式。我们怎么改变数据库中的的UserManager存储用户的数据?
静态启动()
{
PublicClientId =自我; UserManagerFactory =()=>新的UserManager&所述; IdentityUser>(新UserStore&所述; IdentityUser>()); OAuthOptions =新OAuthAuthorizationServerOptions
{
TokenEndpointPath =新PathString(/令牌),
供应商=新ApplicationOAuthProvider(PublicClientId,UserManagerFactory)
AuthorizeEndpointPath =新PathString(/ API /帐号/ ExternalLogin),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp =真
};
}
此外什么@ ta.speot.is和@Shaun提到:您也可以通过在连接字符串(存储在web.config)的名称上下文的IdentityDbContext的基构造
公共类MyDbContext:IdentityDbContext<&MYUSER GT;
{
公共MyDbContext()
:基地(TheNameOfTheConnectionString)
{
}
}
这<一个href=\"http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx\">tutorial含有丰富的例子。
另一种方法是使用连接字符串的名称上下文的构造函数的参数,并把它传递给基构造。
We have created a new ASP.NET 4.5.1 project as follows:
- Visual Studio 2013
- New Project
- Visual C#
- Web
- ASP.NET Web Application
- Web API
- Change Authentication
- Individual User Accounts
- Okay > Okay
In the solution explorer > App_Start > Startup.Auth.cs file there is the following code which configures ASP.NET Indentity. How do we change the database in which the UserManager stores user data?
static Startup()
{
PublicClientId = "self";
UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
}
Additionally to what @ta.speot.is and @Shaun mentioned: You can also pass the name of the connection string (stored in the web.config) in your context to the base constructor of the IdentityDbContext
public class MyDbContext : IdentityDbContext<MyUser>
{
public MyDbContext()
: base("TheNameOfTheConnectionString")
{
}
}
This tutorial contains an extensive example.
Another way would be to use the name of the connection string as a parameter of your context constructor and pass it to the base constructor.
这篇关于更改在ASP.NET身份存储用户数据的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!