更改ASP.Net标识提供程序的连接字符串
我有一个使用ClaimsEntity的webforms应用程序(不是MVC)。我想动态切换连接字符串,但我看不到这样做的方法。我在网上看到的一切,都是关于MVC的。以下是我的代码:更改ASP.Net标识提供程序的连接字符串,asp.net,vb.net,asp.net-identity,claims-based-identity,Asp.net,Vb.net,Asp.net Identity,Claims Based Identity,我有一个使用ClaimsEntity的webforms应用程序(不是MVC)。我想动态切换连接字符串,但我看不到这样做的方法。我在网上看到的一切,都是关于MVC的。以下是我的代码: Dim userStore As New UserStore(Of IdentityUser) Dim roleStore As New RoleStore(Of IdentityRole) Dim userManager As New UserManager(Of IdentityUser)(userStore)
Dim userStore As New UserStore(Of IdentityUser)
Dim roleStore As New RoleStore(Of IdentityRole)
Dim userManager As New UserManager(Of IdentityUser)(userStore)
Dim roleManager As New RoleManager(Of IdentityRole)(roleStore)
Dim userName As String = identity.Claims.First(Function(claim) claim.Type = ClaimTypes.WindowsAccountName).Value
是否需要更改我使用的连接字符串,而不是Web.config文件中的DefaultConnection字符串
编辑
我已尝试创建此类,以从中派生:
Imports System.Data.Entity
Imports Application.Common
Imports Application.Data.Migrations
Imports Application.Models
Imports Microsoft.AspNet.Identity.EntityFramework
Namespace Application.Data
Public Class ApplicationDbContext
Inherits IdentityDbContext(Of IdentityUser)
Public Sub New(ByVal stringName As String)
MyBase.New(stringName, throwIfV1Schema:=False)
End Sub
Public Shared Function Create(ByVal stringName As String) As ApplicationDbContext
Return New ApplicationDbContext(stringName)
End Function
Public Overloads Function [Set](Of TEntity As Class)() As IDbSet(Of TEntity)
Return MyBase.[Set](Of TEntity)()
End Function
End Class
End Namespace
然后,我这样做:
Dim newContext As ApplicationDbContext = New ApplicationDbContext("test")
Dim userStore As New UserStore(newContext)
Dim roleStore As New RoleStore(newContext)
Dim userManager As New UserManager(Of IdentityUser)(userStore)
Dim roleManager As New RoleManager(Of IdentityRole)(roleStore)
但是,变量userStore和RoleStore出现了一个错误“对‘userStore’的一些参数…”
编辑
像这样创建管理者是有效的:
Dim userStore = New UserStore(Of IdentityUser)(New ApplicationDbContext("DefaultConnection1"))
Dim roleStore = New RoleStore(Of IdentityRole)(New ApplicationDbContext("DefaultConnection1"))
您可以创建一个
IdentityDbContext
实例,并通过构造函数设置ConnectionString,然后使用此实例创建UserStore
和RoleStore
,最后使用它们创建UserManager
和RoleManager
我不太擅长VB
,以下是我在C#
我已经尝试了您的建议,但我不确定我的新类是否正确实现。当我使用它时,我会得到一个错误。有关更多信息,请参阅我的更新。你能告诉我我的新ApplicationBContext类应该包括什么吗?我遗漏了什么吗?如果是C#,那没关系。我能翻译,我刚修好。我必须这样实现:Dim userStore=newuserstore(属于IdentityUser)(newapplicationdbcontext(“DefaultConnection1”))Dim roleStore=newrolestore(属于IdentityRole)(newapplicationdbcontext(“DefaultConnection1”))
string nameOrConnectionString = "YOUR_CONNECTION_STRING";
var dbcontext = new IdentityDbContext(nameOrConnectionString);
var userStore = new UserStore(dbcontext);
var roleStore = new RoleStore(dbcontext);
var UserManager = new UserManager(roleStore);
var RoleManager = new RoleManager(roleStore);