11using System ;
2- using System . Collections . Generic ;
3- using Microsoft . Extensions . DependencyInjection ;
42using ServiceStack ;
53using ServiceStack . Web ;
64using ServiceStack . Data ;
75using ServiceStack . Auth ;
86using ServiceStack . Configuration ;
9- using ServiceStack . OrmLite ;
7+
8+ [ assembly: HostingStartup ( typeof ( MyApp . ConfigureAuthRepository ) ) ]
109
1110namespace MyApp
1211{
@@ -20,42 +19,35 @@ public class AppUser : UserAuth
2019
2120 public class AppUserAuthEvents : AuthEvents
2221 {
23- public override void OnAuthenticated ( IRequest req , IAuthSession session , IServiceBase authService ,
24- IAuthTokens tokens , Dictionary < string , string > authInfo )
22+ public override async Task OnAuthenticatedAsync ( IRequest httpReq , IAuthSession session , IServiceBase authService ,
23+ IAuthTokens tokens , Dictionary < string , string > authInfo , CancellationToken token = default )
2524 {
26- var authRepo = HostContext . AppHost . GetAuthRepository ( req ) ;
25+ var authRepo = HostContext . AppHost . GetAuthRepositoryAsync ( httpReq ) ;
2726 using ( authRepo as IDisposable )
2827 {
29- var userAuth = ( AppUser ) authRepo . GetUserAuth ( session . UserAuthId ) ;
28+ var userAuth = ( AppUser ) await authRepo . GetUserAuthAsync ( session . UserAuthId , token ) ;
3029 userAuth . ProfileUrl = session . GetProfileUrl ( ) ;
31- userAuth . LastLoginIp = req . UserHostAddress ;
30+ userAuth . LastLoginIp = httpReq . UserHostAddress ;
3231 userAuth . LastLoginDate = DateTime . UtcNow ;
33- authRepo . SaveUserAuth ( userAuth ) ;
32+ await authRepo . SaveUserAuthAsync ( userAuth , token ) ;
3433 }
3534 }
3635 }
3736
38- public class ConfigureAuthRepository : IConfigureAppHost , IConfigureServices , IPreInitPlugin
37+ public class ConfigureAuthRepository : IHostingStartup
3938 {
40- public void Configure ( IServiceCollection services )
41- {
42- services . AddSingleton < IAuthRepository > ( c =>
39+ public void Configure ( IWebHostBuilder builder ) => builder
40+ . ConfigureServices ( services => services . AddSingleton < IAuthRepository > ( c =>
4341 new OrmLiteAuthRepository < AppUser , UserAuthDetails > ( c . Resolve < IDbConnectionFactory > ( ) ) {
4442 UseDistinctRoleTables = true
45- } ) ;
46- }
47-
48- public void Configure ( IAppHost appHost )
49- {
50- var authRepo = appHost . Resolve < IAuthRepository > ( ) ;
51- authRepo . InitSchema ( ) ;
52- CreateUser ( authRepo , "admin@email.com" , "Admin User" , "p@55wOrd" , roles : new [ ] { RoleNames . Admin } ) ;
53- }
54-
55- public void BeforePluginsLoaded ( IAppHost appHost )
56- {
57- appHost . AssertPlugin < AuthFeature > ( ) . AuthEvents . Add ( new AppUserAuthEvents ( ) ) ;
58- }
43+ } ) )
44+ . ConfigureAppHost ( appHost => {
45+ var authRepo = appHost . Resolve < IAuthRepository > ( ) ;
46+ authRepo . InitSchema ( ) ;
47+ CreateUser ( authRepo , "admin@email.com" , "Admin User" , "p@55wOrd" , roles : new [ ] { RoleNames . Admin } ) ;
48+ } , afterConfigure : appHost => {
49+ appHost . AssertPlugin < AuthFeature > ( ) . AuthEvents . Add ( new AppUserAuthEvents ( ) ) ;
50+ } ) ;
5951
6052 // Add initial Users to the configured Auth Repository
6153 public void CreateUser ( IAuthRepository authRepo , string email , string name , string password , string [ ] roles )
0 commit comments