Created
November 11, 2021 15:04
-
-
Save gistlyn/eac7beb7f88d8d9387e1204c56deeec3 to your computer and use it in GitHub Desktop.
Configure AuthFeature inc. .NET Core Providers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet add package ServiceStack.Extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 CredentialsAuthProvider(appSettings), /* Sign In with Username / Password credentials */ | |
new AppleAuthProvider(appSettings), /* Configure: https://developer.okta.com/blog/2019/06/04/what-the-heck-is-sign-in-with-apple */ | |
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>>(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment