forked from aspnet/Identity
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathStartup.cs
139 lines (119 loc) · 3.9 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.Data.SqlClient;
using System.IO;
using IdentitySample.Models;
using IdentitySample.Services;
using LinqToDB;
using LinqToDB.Data;
using LinqToDB.DataProvider.SqlServer;
using LinqToDB.Identity;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace IdentitySample
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true);
//if (env.IsDevelopment())
// builder.AddUserSecrets();
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Set connection configuration
DataConnection
.AddConfiguration(
"Default",
Configuration["Data:DefaultConnection:ConnectionString"],
SqlServerTools.GetDataProvider(SqlServerVersion.v2012, SqlServerProvider.SystemDataSqlClient));
DataConnection.DefaultConfiguration = "Default";
services.AddIdentity<ApplicationUser, LinqToDB.Identity.IdentityRole>()
.AddLinqToDBStores(new DefaultConnectionFactory())
.AddDefaultTokenProviders();
services.AddAuthentication()
.AddCookie(options =>
{
options.Cookie.Name = "Interop";
options.DataProtectionProvider =
DataProtectionProvider.Create(new DirectoryInfo("C:\\Github\\Identity\\artifacts"));
});
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseExceptionHandler("/Home/Error");
var connectionString = new SqlConnectionStringBuilder(Configuration["Data:DefaultConnection:ConnectionString"])
{
InitialCatalog = "master"
}.ConnectionString;
using (var db = new DataConnection(SqlServerTools.GetDataProvider(), connectionString))
{
try
{
var sql = "create database [" +
new SqlConnectionStringBuilder(Configuration["Data:DefaultConnection:ConnectionString"])
.InitialCatalog + "]";
db.Execute(sql);
}
catch
{
//
}
}
// Try to create tables
using (var db = new ApplicationDataConnection())
{
TryCreateTable<ApplicationUser>(db);
TryCreateTable<LinqToDB.Identity.IdentityRole>(db);
TryCreateTable<LinqToDB.Identity.IdentityUserClaim<string>>(db);
TryCreateTable<LinqToDB.Identity.IdentityRoleClaim<string>>(db);
TryCreateTable<LinqToDB.Identity.IdentityUserLogin<string>>(db);
TryCreateTable<LinqToDB.Identity.IdentityUserRole<string>>(db);
TryCreateTable<LinqToDB.Identity.IdentityUserToken<string>>(db);
}
app.UseStaticFiles();
app.UseAuthentication();
// To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715
app.UseMvc(routes =>
{
routes.MapRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
});
}
private void TryCreateTable<T>(ApplicationDataConnection db)
where T : class
{
try
{
db.CreateTable<T>();
}
catch
{
//
}
}
}
}