use MSSQLLocalDB and create demo application if not exist

This commit is contained in:
Yong Zhao 2020-09-24 15:57:10 -04:00
Родитель 5b046fa0cb
Коммит e289924024
4 изменённых файлов: 108 добавлений и 62 удалений

Просмотреть файл

@ -51,9 +51,30 @@ namespace SQLMembership_Identity_OWIN.IdentityAcccount
{
return reader.GetGuid(0);
}
return Guid.NewGuid();
connection.Close();
return createApplication(connection, "/");
}
}
private Guid createApplication(SqlConnection sqlConnection, string s)
{
var appid = Guid.NewGuid();
var insStatement =
$"INSERT INTO [dbo].aspnet_Applications" +
$"([ApplicationName], [LoweredApplicationName], [ApplicationId], [Description]) " +
$"VALUES (@appName, @lowAppName, @appId, 'SQLMembership-Identity-OWIN')";
var sqlcmd = new SqlCommand(insStatement, sqlConnection);
sqlcmd.Connection.Open();
sqlcmd.Parameters.AddWithValue("@appId", appid);
sqlcmd.Parameters.AddWithValue("@appName", s);
sqlcmd.Parameters.AddWithValue("@lowAppName",
(s ?? "").ToLower());
var rc = sqlcmd.ExecuteNonQuery();
Console.WriteLine($"insert application returns {rc}");
return appid;
}
}
}

Просмотреть файл

@ -96,10 +96,25 @@ namespace SQLMembership_Identity_OWIN.IdentityAccount
{
return;
}
var currentApplicationId = new ApplicationDbContext().Applications.SingleOrDefault(x => x.ApplicationName == "/").ApplicationId;
Guid? currentApplicationId = null;
using (var ctx = new ApplicationDbContext())
{
currentApplicationId = new ApplicationDbContext().Applications
.SingleOrDefault(x => x.ApplicationName == "/")?.ApplicationId;
if (currentApplicationId == null)
{
ctx.Applications.Add(new Application()
{
ApplicationId = (Guid) (currentApplicationId = Guid.NewGuid()),
ApplicationName = "/"
});
ctx.SaveChanges();
}
}
var manager = new UserManager();
var user = new User() { UserName = Username.Text, ApplicationId = currentApplicationId, LoweredUserName = Username.Text.ToLower() };
var user = new User() { UserName = Username.Text, ApplicationId = (Guid) currentApplicationId, LoweredUserName = Username.Text.ToLower() };
IdentityResult result = manager.Create(user);

Просмотреть файл

@ -0,0 +1,7 @@
<#
create the required aspnetdb database
#>
$dbfile = "${PSScriptRoot}\App_Data\aspnetdb.mdf"
Write-Host "Creating aspnetdb on $dbfile"
& osql -S "(localdb)\MSSQLLocalDB" -E -Q "Create DATABASE [aspnetdb] ON PRIMARY (NAME='aspnetdb', FILENAME='$dbfile')"

Просмотреть файл

@ -1,59 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;" providerName="System.Data.SqlClient" />
</connectionStrings>
<!--
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
The following attributes can be set on the <httpRuntime> tag.
<system.Web>
<httpRuntime targetFramework="4.5.1" />
</system.Web>
-->
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<authentication mode="None" />
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
<pages controlRenderingCompatibilityVersion="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=(LocalDb)\MSSQLLocalDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<!--
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
The following attributes can be set on the <httpRuntime> tag.
<system.Web>
<httpRuntime targetFramework="4.5.1" />
</system.Web>
-->
<system.web>
<compilation debug="true" targetFramework="4.5.1"/>
<authentication mode="None"/>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear/>
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/>
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider"/>
</providers>
</roleManager>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>