更改 ASP.Net 身份提供商的连接字符串

Change connection string for ASP.Net Identity Provider

我有一个使用 ClaimsIdentity 的 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 roleManager As New RoleManager(Of IdentityRole)(roleStore)

Dim userName As String = identity.Claims.First(Function(claim) claim.Type = ClaimTypes.WindowsAccountName).Value 

是否可以更改我正在使用的连接字符串,而不是 Web.config 文件中的 DefaultConnection 字符串?

编辑

我已经尝试创建这个 class,派生自:

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出错了"To few arguements to 'UserStore' ..."

编辑

像这样创建管理器,成功了:

 Dim userStore = New UserStore(Of IdentityUser)(New ApplicationDbContext("DefaultConnection1"))
 Dim roleStore = New RoleStore(Of IdentityRole)(New ApplicationDbContext("DefaultConnection1"))

您可以创建一个 IdentityDbContext 实例并通过构造函数设置 ConnectionString,然后使用该实例创建 UserStoreRoleStore,最后使用它们创建 UserManagerRoleManager.

我不太擅长 VB,这就是我在 C#

中的意思
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);