The client-side code is as simple as this:
import { JsonServiceClient} from '@servicestack/client';
var client = new JsonServiceClient('http://localhost:45001');
client.mode = "no-cors";
var authenticate = new requests.security.AuthenticateRequest();
authenticate.UserName = username;
authenticate.Password = password;
client.postToUrl("/auth/credentials?format=json", authenticate).then((response => {
this.doGetCurrentUser().then((user) => {
userState.setUser(user);
}).catch((error) => {
this.showError("There was a problem authenticating your details");
});
})).catch((error) => {
this.showError("There was a problem authenticating your details");
});
and the server-side configuration in Startup.cs is:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) => Configuration = configuration;
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(builder =>
builder.WithOrigins("http://localhost:45000").AllowAnyHeader().AllowAnyMethod().AllowCredentials()
);
app.UseServiceStack(new AppHost
{
AppSettings = new NetCoreAppSettings(Configuration)
});
}
}
public class AppHost : AppHostBase
{
public AppHost() : base("Micromni.Core.Service", typeof(Services).Assembly) { }
// Configure your AppHost with the necessary configuration and dependencies your App needs
public override void Configure(Container container)
{
Plugins.Add(new TemplatePagesFeature()); // enable server-side rendering, see: http://templates.servicestack.net
Plugins.Add(new CorsFeature(
allowCredentials: true,
allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
allowedHeaders: "Content-Type, Allow, Authorization, Origin",
allowOriginWhitelist: new[] {
"http://localhost:45000"
}));
SetConfig(new HostConfig
{
AddRedirectParamsToQueryString = true,
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
});
ConfigureAuth(container);
}
private void ConfigureAuth(Container container)
{
Plugins.Add(new AuthFeature(
() => new AuthUserSession(),
new IAuthProvider[] {
new JwtAuthProvider() { HashAlgorithm = "RS256", PrivateKeyXml = AppSettings.GetString("PrivateKeyXml"), RequireSecureConnection = false },
//new JwtAuthProvider() { AuthKey = AesUtils.CreateKey(), RequireSecureConnection = false },
new CustomCredentialsAuthProvider(this, AppSettings)
}));
container.Register<ICacheClient>(new MemoryCacheClient());
var userRep = new InMemoryAuthRepository();
container.Register<IUserAuthRepository>(userRep);
}
}
Like I said if I intercept the request with Fiddler and change the Content-Type back to application/json the server receives the request and hits my breakpoint with no problem.
I am using “@servicestack/client”: “^1.0.14”, on my client site and ServiceStack (5.0.2) on the web service.