Reputation: 3615
I have a webforms application (Not MVC) that uses ClaimsIdentity. I would like to dynamically switch connection strings, but I do not see a way to do this. Everything I have seen on line, that references this, seems to be about MVC. Here is the code I have:
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
Is there anyway to change which connection string I am using, instead of the DefaultConnection string found in the Web.config file?
EDIT
I have tried creating this class, to derive from:
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
Then, I do this:
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)
But, the variables userStore and RoleStore of an error "To few arguements to 'UserStore' ..."
EDIT
Creating the managers, like this, worked:
Dim userStore = New UserStore(Of IdentityUser)(New ApplicationDbContext("DefaultConnection1"))
Dim roleStore = New RoleStore(Of IdentityRole)(New ApplicationDbContext("DefaultConnection1"))
Upvotes: 0
Views: 1026
Reputation: 15005
You can create a IdentityDbContext
instance and set ConnectionString via constructor and then create UserStore
and RoleStore
with this instance and finally use them for creating UserManager
and RoleManager
.
I'm not so good at VB
, here's what I mean in 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);
Upvotes: 1