Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active June 21, 2019 22:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gistlyn/1ec54e10d44f87e0f20daaf7e2248fea to your computer and use it in GitHub Desktop.
Configure ServiceStack AuthFeature
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.FluentValidation;
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 : IConfigureAppHost, IConfigureServices
{
public void Configure(IServiceCollection services)
{
//services.AddSingleton<ICacheClient>(new MemoryCacheClient()); //Store User Sessions in Memory Cache (default)
}
public void Configure(IAppHost appHost)
{
var AppSettings = appHost.AppSettings;
appHost.Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
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>>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment