Skip to content
Permalink
Browse files

Use latest modular auth-memory AuthRepository

  • Loading branch information...
mythz committed Jun 26, 2019
1 parent d8616e9 commit 278a965d3b8d922f6ea55408269308ff90913263
Showing with 60 additions and 31 deletions.
  1. +19 −24 MyApp/Configure.Auth.cs
  2. +41 −7 MyApp/Configure.AuthRepository.cs
@@ -1,15 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Funq;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.Configuration;
using ServiceStack.FluentValidation;

namespace MyApp
{
[Priority(-100)] // Run before ConfigureAuthRepository
// 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)
@@ -34,22 +47,4 @@ public void Configure(IAppHost appHost)
appHost.RegisterAs<CustomRegistrationValidator, IValidator<Register>>();
}
}

// 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();
});
}
}
}
}
@@ -1,31 +1,65 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Web;
using ServiceStack.Auth;
using ServiceStack.Configuration;

namespace MyApp
{
[Priority(-80)] // Run before AppHost.Configure()
public class ConfigureAuthRepository : IConfigureAppHost, IConfigureServices
// Custom UserAuth Data Model with extended Metadata properties
public class AppUser : UserAuth
{
public string ProfileUrl { get; set; }
public string LastLoginIp { get; set; }
public DateTime? LastLoginDate { get; set; }
}

public class AppUserAuthEvents : AuthEvents
{
public override void OnAuthenticated(IRequest req, IAuthSession session, IServiceBase authService,
IAuthTokens tokens, Dictionary<string, string> authInfo)
{
var authRepo = HostContext.AppHost.GetAuthRepository(req);
using (authRepo as IDisposable)
{
var userAuth = (AppUser)authRepo.GetUserAuth(session.UserAuthId);
userAuth.ProfileUrl = session.GetProfileUrl();
userAuth.LastLoginIp = req.UserHostAddress;
userAuth.LastLoginDate = DateTime.UtcNow;
authRepo.SaveUserAuth(userAuth);
}
}
}

public class ConfigureAuthRepository : IConfigureAppHost, IConfigureServices, IPreInitPlugin
{
public void Configure(IServiceCollection services)
{
services.AddSingleton<IAuthRepository>(new InMemoryAuthRepository()); //Store Authenticated Users in Memory
services.AddSingleton<IAuthRepository>(c =>
new InMemoryAuthRepository<AppUser, UserAuthDetails>());
}

public void Configure(IAppHost appHost)
{
CreateUser(appHost.Resolve<IAuthRepository>(),
"admin@email.com", "Admin User", "p@55wOrd", roles:new[]{ RoleNames.Admin });
var authRepo = appHost.Resolve<IAuthRepository>();
authRepo.InitSchema();

CreateUser(authRepo, "admin@email.com", "Admin User", "p@55wOrd", roles:new[]{ RoleNames.Admin });
}

public void BeforePluginsLoaded(IAppHost appHost)
{
appHost.AssertPlugin<AuthFeature>().AuthEvents.Add(new AppUserAuthEvents());
}

// Add initial Users to the configured Auth Repository
public void CreateUser(IAuthRepository authRepo, string email, string name, string password, string[] roles)
{
if (authRepo.GetUserAuthByUserName(email) == null)
{
var newAdmin = new UserAuth { Email = email, DisplayName = name };
var newAdmin = new AppUser { Email = email, DisplayName = name };
var user = authRepo.CreateUserAuth(newAdmin, password);
authRepo.AssignRoles(user, roles);
}

0 comments on commit 278a965

Please sign in to comment.
You can’t perform that action at this time.