我有一个使用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字符串?
编辑
我尝试创建此类,以派生自:
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'的争论不休...”的变量userStore和RoleStore
编辑
像这样创建经理很成功:
Dim userStore = New UserStore(Of IdentityUser)(New ApplicationDbContext("DefaultConnection1"))
Dim roleStore = New RoleStore(Of IdentityRole)(New ApplicationDbContext("DefaultConnection1"))
答案 0 :(得分:1)
您可以创建一个IdentityDbContext
实例并通过构造函数设置ConnectionString,然后使用该实例创建UserStore
和RoleStore
,最后使用它们来创建UserManager
和{{1 }}。
我对RoleManager
不太擅长,这就是我在VB
中的意思
C#