I am using Servicestack 5.13.3 and am having a strange issue where base.GetSession() does not fill the UserAuthId and show the session as Unauthenticated. I have checked the ss-id and ss-pid and it is set when I log in. I can also see it being passed back to my service, but inspecting the session shows it as not being authenticated. I also looked at the session Id to make sure that the session exist in the redis cache and it does and show in the cache as authenticated, although the roles are not filled.
{
“__type”: “ServiceStack.AuthUserSession, ServiceStack”,
“id”: “v0Ir0YpFCITBim4yk2AN”,
“user_auth_id”: “1”,
“user_auth_name”: “andy.fensham@scadsoftware.com”,
“display_name”: “Admin User”,
“email”: “andy.fensham@scadsoftware.com”,
“created_at”: “/Date(-62135596800000-0000)/”,
“last_modified”: “/Date(1638380490522)/”,
“roles”: ,
“permissions”: ,
“is_authenticated”: true,
“from_token”: false,
“profile_url”: “data:image/svg+xml,%3Csvg width=‘100’ height=‘100’ viewBox=‘0 0 100 100’ xmlns=‘SVG namespace’%3E %3Cstyle%3E .path%7B%7D %3C/style%3E %3Cg id=‘male-svg’%3E%3Cpath fill=‘%23556080’ d=‘M1 92.84V84.14C1 84.14 2.38 78.81 8.81 77.16C8.81 77.16 19.16 73.37 27.26 69.85C31.46 68.02 32.36 66.93 36.59 65.06C36.59 65.06 37.03 62.9 36.87 61.6H40.18C40.18 61.6 40.93 62.05 40.18 56.94C40.18 56.94 35.63 55.78 35.45 47.66C35.45 47.66 32.41 48.68 32.22 43.76C32.1 40.42 29.52 37.52 33.23 35.12L31.35 30.02C31.35 30.02 28.08 9.51 38.95 12.54C34.36 7.06 64.93 1.59 66.91 18.96C66.91 18.96 68.33 28.35 66.91 34.77C66.91 34.77 71.38 34.25 68.39 42.84C68.39 42.84 66.75 49.01 64.23 47.62C64.23 47.62 64.65 55.43 60.68 56.76C60.68 56.76 60.96 60.92 60.96 61.2L64.74 61.76C64.74 61.76 64.17 65.16 64.84 65.54C64.84 65.54 69.32 68.61 74.66 69.98C84.96 72.62 97.96 77.16 97.96 81.13C97.96 81.13 99 86.42 99 92.85L1 92.84Z’/%3E%3C/g%3E%3C/svg%3E”,
“tag”: 0,
“auth_provider”: “credentials”,
“provider_o_auth_access”:
}
However in session = base.GetSession(), it shows isAuthenticated as false and UserAuthId is not filled.
I have a standard Configure.Auth.cs I pulled in with mix that looks like.
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.FluentValidation;
namespace digitaluapi
{
// Add any additional metadata properties you want to store in the Users Typed Session
//public class CustomUserSession : AuthUserSession
//{
// public int TenantsId { get; set; }
//}
// 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 */
// }));
var AppSettings = appHost.AppSettings;
appHost.Plugins.Add(new AuthFeature(
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>>();
}
}
}
And a Configure.AuthRepository.cs that looks like
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Web;
using ServiceStack.Data;
using ServiceStack.Auth;
using ServiceStack.Configuration;
using ServiceStack.OrmLite;
using digitaluapi.ServiceModel.Types;
namespace digitaluapi
{
// Custom User Table with extended Metadata properties
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>(c =>
new OrmLiteAuthRepository<AppUser, UserAuthDetails>(c.Resolve<IDbConnectionFactory>()) {
UseDistinctRoleTables = true
});
}
public void Configure(IAppHost appHost)
{
var authRepo = appHost.Resolve<IAuthRepository>();
authRepo.InitSchema();
CreateUser(authRepo, "andy.fensham@scadsoftware.com", "Admin User", "xxxxxxxxxxx", roles: new[] { RoleNames.Admin });
CreateUser(authRepo, "alwyn.pelzer@scadsoftware.com", "Admin User", "xxxxxxxxxxx", roles: new[] { RoleNames.Admin });
CreateUser(authRepo, "jmaguranye@gmail.com", "Admin User", "xxxxxxxxxxxx", roles: new[] { RoleNames.Admin });
CreateUser(authRepo, "jzheke@gmail.com", "Admin User", "xxxxxxxxxxx", roles: new[] { RoleNames.Admin });
CreateUser(authRepo, "dawie.martins@scadsoftware.com", "Admin User", "xxxxxxxxxxxx", 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);
}
}
}
}
Please help.