-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigure.Auth.cs
53 lines (47 loc) · 2.37 KB
/
Configure.Auth.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
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.FluentValidation;
[assembly: HostingStartup(typeof(MyApp.ConfigureAuth))]
namespace MyApp
{
// Add any additional metadata properties you want to store in the Users Typed Session
public class CustomUserSession : AuthUserSession
{
}
// Custom Validator to add custom validators to built-in /register Service requiring DisplayName and ConfirmPassword
public class CustomRegistrationValidator : RegistrationValidator
{
public CustomRegistrationValidator()
{
RuleSet(ApplyTo.Post, () =>
{
RuleFor(x => x.DisplayName).NotEmpty();
RuleFor(x => x.ConfirmPassword).NotEmpty();
});
}
}
public class ConfigureAuth : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices(services => {
//services.AddSingleton<ICacheClient>(new MemoryCacheClient()); //Store User Sessions in Memory Cache (default)
})
.ConfigureAppHost(appHost => {
var appSettings = appHost.AppSettings;
appHost.Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new NetCoreIdentityAuthProvider(appSettings) {
AdminRoles = { "Manager" }, // Automatically Assign additional roles to Admin Users
}, /* Use ServiceStack Auth in MVC */
new CredentialsAuthProvider(appSettings), /* Sign In with Username / Password credentials */
new FacebookAuthProvider(appSettings), /* Create App https://developers.facebook.com/apps */
new GoogleAuthProvider(appSettings), /* Create App https://console.developers.google.com/apis/credentials */
new MicrosoftGraphAuthProvider(appSettings), /* Create App https://apps.dev.microsoft.com */
}));
appHost.Plugins.Add(new RegistrationFeature()); //Enable /register Service
//override the default registration validation with your own custom implementation
appHost.RegisterAs<CustomRegistrationValidator, IValidator<Register>>();
});
}
}