If you are using the SimpleMembershipProvider for FormAuthentication in a ASP .NET MVC4 / Web Api project, the following PowerShell / C# code can be used to create and delete users and roles.

  • Create an empty XML App.config file "C:\Temp\App.config".
  • Paste the XML below in the file and save it.
  • Create an empty PowerShell file "C:\Temp\Manage_MVC_users_and_roles.ps1".
  • Paste the PowerShell code below in the file and save it.
  • Create an empty C# file "C:\Temp\Manage_MVC_users_and_roles.cs".
  • Paste the C# code below in the file and save it.
  • Execute the file "C:\Temp\Manage_MVC_users_and_roles.ps1" with PowerShell.

 

This will create an user "test2" with password "test2" and a role "Administrators".

 

Note

  • Database tables will automatically be created if they don’t exist.
  • De folder "C:\Temp" should contain the assemblies "System.Web.WebPages.dll" and "WebMatrix.Data.dll" and "WebMatrix.WebData.dll", this assembly can be downloaded using NuGet.

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <profile defaultProvider="SimpleProfileProvider">
      <providers>
        <add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"
            connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
      <providers>
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
      </providers>
    </roleManager>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    </assemblyBinding>
  </runtime>
</configuration>

PowerShell code (Manage_MVC_users_and_roles.ps1)

 
# Get folder containing this script.
$scriptFolder = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent

# Load App.config file from scriptfolder.
$appConfigPath = "$scriptFolder\App.config"
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $appConfigPath)

# Compile C# code to dll.
$Assem = ( 
    "WebMatrix.Data", 
    "WebMatrix.WebData",
    "System.Security",
    "System.Web",
    "System.Web.WebPages",
    'System.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35',
    'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
)
$codePath = "$scriptFolder\Manage_MVC_users_and_roles.cs"
$codeAssemblyName = "Manage_MVC_users_and_roles.dll"
Add-Type -OutputType Library –ReferencedAssemblies $Assem -OutputAssembly $codeAssemblyName -Path $codePath

# Load dll.
$codeAssemblyPath = "$scriptFolder\$codeAssemblyName"
Add-Type -Path $codeAssemblyPath

# Execute C# code.
$wrapper = New-Object Research.Rli.WebSecurityExecuter
$wrapper.Execute($scriptFolder);

pause
 

C# code

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.