Last active
April 9, 2021 07:46
-
-
Save gistlyn/e9a38f7f04ef23e94b7251d95ca45f25 to your computer and use it in GitHub Desktop.
Configure Marten Auth Repository
This file contains hidden or 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.Authentication.Marten |
This file contains hidden or 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 System; | |
using System.Collections.Generic; | |
using Microsoft.Extensions.DependencyInjection; | |
using ServiceStack; | |
using ServiceStack.Web; | |
using ServiceStack.Auth; | |
using ServiceStack.Configuration; | |
using Marten; | |
using ServiceStack.Authentication.Marten; | |
namespace MyApp | |
{ | |
// 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 | |
{ | |
static ConfigureAuthRepository() | |
{ | |
ConfigureMarten.Options.Add(opts => { | |
opts.AuthRepository() | |
.UseAuth<UserAuth>() | |
.UseAuthDetails<UserAuthDetails>(); | |
}); | |
} | |
public void Configure(IServiceCollection services) | |
{ | |
services.AddSingleton<IAuthRepository>(c => | |
new MartenAuthRepository<AppUser, UserAuthDetails>(c.Resolve<IDocumentStore>())); | |
} | |
public void Configure(IAppHost appHost) | |
{ | |
var authRepo = appHost.Resolve<IAuthRepository>(); | |
authRepo.InitSchema(); | |
//CreateUser(authRepo, "[email protected]", "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 AppUser { Email = email, DisplayName = name }; | |
var user = authRepo.CreateUserAuth(newAdmin, password); | |
authRepo.AssignRoles(user, roles); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment