This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 239
/
Startup.cs
192 lines (167 loc) · 7.06 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using Autofac;
using Autofac.Integration.Mvc;
using BrockAllen.MembershipReboot;
using BrockAllen.MembershipReboot.Ef;
using BrockAllen.MembershipReboot.Owin;
using BrockAllen.MembershipReboot.Relational;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Facebook;
using Microsoft.Owin.Security.Google;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
namespace SingleTenantOwinSystemWeb
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "External",
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive
});
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
AppId = "260581164087472", AppSecret = "7389d78e6e629954a710351830d080f3",
SignInAsAuthenticationType = "External"
});
app.UseGoogleAuthentication(new GoogleAuthenticationOptions
{
SignInAsAuthenticationType="External"
});
ConfigureMembershipReboot(app);
}
private static void ConfigureMembershipReboot(IAppBuilder app)
{
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<DefaultMembershipRebootDatabase, BrockAllen.MembershipReboot.Ef.Migrations.Configuration>());
//System.Data.Entity.Database.SetInitializer(new System.Data.Entity.CreateDatabaseIfNotExists<DefaultMembershipRebootDatabase>());
var cookieOptions = new CookieAuthenticationOptions
{
AuthenticationType = MembershipRebootOwinConstants.AuthenticationType
};
BuildAutofacContainer(app, cookieOptions.AuthenticationType);
app.UseMembershipReboot(cookieOptions);
}
private static void BuildAutofacContainer(IAppBuilder app, string authType)
{
var builder = new ContainerBuilder();
var config = CreateMembershipRebootConfiguration(app);
builder.RegisterInstance(config).As<MembershipRebootConfiguration>();
builder.RegisterType<DefaultMembershipRebootDatabase>()
.InstancePerLifetimeScope();
builder.RegisterType<DefaultUserAccountRepository>()
.As<IUserAccountRepository>()
.As<IUserAccountRepository<RelationalUserAccount>>()
.As<IUserAccountQuery>()
.As<IUserAccountQuery<BrockAllen.MembershipReboot.Relational.RelationalUserAccount>>()
.InstancePerLifetimeScope();
builder.RegisterType<UserAccountService>().OnActivating(e =>
{
var owin = e.Context.Resolve<IOwinContext>();
var debugging = false;
#if DEBUG
debugging = true;
#endif
e.Instance.ConfigureTwoFactorAuthenticationCookies(owin.Environment, debugging);
})
.AsSelf()
.InstancePerLifetimeScope();
builder.RegisterType<UserAccountService<RelationalUserAccount>>().OnActivating(e =>
{
var owin = e.Context.Resolve<IOwinContext>();
var debugging = false;
#if DEBUG
debugging = true;
#endif
e.Instance.ConfigureTwoFactorAuthenticationCookies(owin.Environment, debugging);
})
.AsSelf()
.InstancePerLifetimeScope();
builder.Register(ctx =>
{
var owin = ctx.Resolve<IOwinContext>();
return new OwinAuthenticationService(authType, ctx.Resolve<UserAccountService>(), owin.Environment);
})
.As<AuthenticationService>()
.InstancePerLifetimeScope();
builder.Register(ctx=>HttpContext.Current.GetOwinContext()).As<IOwinContext>();
builder.RegisterControllers(typeof(Startup).Assembly);
var container = builder.Build();
System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
private static MembershipRebootConfiguration CreateMembershipRebootConfiguration(IAppBuilder app)
{
var config = new MembershipRebootConfiguration();
config.RequireAccountVerification = false;
config.AddEventHandler(new DebuggerEventHandler());
var appInfo = new OwinApplicationInformation(
app,
"Test",
"Test Email Signature",
"/UserAccount/Login",
"/UserAccount/ChangeEmail/Confirm/",
"/UserAccount/Register/Cancel/",
"/UserAccount/PasswordReset/Confirm/");
var emailFormatter = new EmailMessageFormatter(appInfo);
// uncomment if you want email notifications -- also update smtp settings in web.config
config.AddEventHandler(new EmailAccountEventsHandler(emailFormatter));
// uncomment to enable SMS notifications -- also update TwilloSmsEventHandler class below
//config.AddEventHandler(new TwilloSmsEventHandler(appinfo));
// uncomment to ensure proper password complexity
//config.ConfigurePasswordComplexity();
return config;
}
}
public class TwilloSmsEventHandler : SmsEventHandler
{
const string sid = "";
const string token = "";
const string fromPhone = "";
public TwilloSmsEventHandler(ApplicationInformation appInfo)
: base(new SmsMessageFormatter(appInfo))
{
}
string Url
{
get
{
return String.Format("https://api.twilio.com/2010-04-01/Accounts/{0}/SMS/Messages", sid);
}
}
string BasicAuthToken
{
get
{
var val = sid + ":" + token;
var bytes = System.Text.Encoding.UTF8.GetBytes(val);
val = Convert.ToBase64String(bytes);
return val;
}
}
HttpContent GetBody(Message msg)
{
var values = new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("From", fromPhone),
new KeyValuePair<string, string>("To", msg.To),
new KeyValuePair<string, string>("Body", msg.Body),
};
return new FormUrlEncodedContent(values);
}
protected override void SendSms(Message message)
{
if (!String.IsNullOrWhiteSpace(sid))
{
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", BasicAuthToken);
var result = client.PostAsync(Url, GetBody(message)).Result;
result.EnsureSuccessStatusCode();
}
}
}
}