I am migrating a retro ServiceStack .NET framework 4.8 REST service project to .NET Core 3.1. I am using custom authentication. Now after copying the relevant parts not any authentication cookies are set in the browser. On the server I see a stored session, so this part is working.
My code:
–> AppHost:
public class AppHost : AppHostBase
{
public AppHost() : base(“Something.Services”, typeof(SomeService).Assembly) { }
// Configure your AppHost with the necessary configuration and dependencies your App needs
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
UseSameSiteCookies = true,
UseSecureCookies = true,
DefaultRedirectPath = "/metadata",
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
});
Plugins.Add((new AuthFeature(() =>
new CustomUserSession(),
new IAuthProvider[] {
new CustomAuthProvider(),
}, "http://www.somesite.com/login")));
}
–> CustomUserSession:
public class CustomAuthProvider : AuthProvider
{
public CustomAuthProvider()
{
this.Provider = "Gloneco Custom AuthProvider";
}
public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
{
return session.IsAuthenticated;
}
public override Task<object> AuthenticateAsync(IServiceBase authService, IAuthSession session, Authenticate request, CancellationToken token = new CancellationToken())
{
throw new NotImplementedException();
}
}
[DataContract]
public class CustomUserSession : AuthUserSession
{
[DataMember]
public string Something { get; set; }
}
–> Validation REST Method (stripped code):
public object Get(ValidateSomethingRequest request)
{
//BUG: No cookies!
base.Request.AddSessionOptions(SessionOptions.Permanent);
var session = base.SessionAs<AuthUserSession>();
session.IsAuthenticated = true;
session.Roles = new List<string>();
session.Roles.Add(user.Role);
this.SaveSession(session, new TimeSpan(_serviceAppSettings.AuthenticationExpirationDays, 0, 0, 0, 0));
}
I checked everything like correct namespace of ‘public AppHost() : base(“Something.Services”, typeof(SomeService).Assembly) { }’ but I am missing something I guess.
SIDENOTE:
VS complains that the ‘SaveSession’ is obsolete and I should use ‘SaveSessionAsync’. I have looked and even tried an mvcauth example from your latest .NET 5 template ‘x’ tool but could not find an async sample for a ‘object Get’. Or is it as simple as ‘async Task< object > Get’?