Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active November 28, 2024 07:45
Show Gist options
  • Save gistlyn/43d864ebe001493834b7f05a2bcc8d2d to your computer and use it in GitHub Desktop.
Save gistlyn/43d864ebe001493834b7f05a2bcc8d2d to your computer and use it in GitHub Desktop.
apikeys-auth
dotnet add package ServiceStack.Server
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.Data;
using ServiceStack.OrmLite;
[assembly: HostingStartup(typeof(MyApp.ConfigureApiKeys))]
namespace MyApp;
public class ConfigureApiKeys : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices(services =>
{
services.AddPlugin(new AuthFeature([
new ApiKeyCredentialsProvider(),
new AuthSecretAuthProvider("p@55wOrd"),
]));
services.AddPlugin(new SessionFeature());
services.AddPlugin(new ApiKeysFeature
{
// Optional: Available Scopes Admin Users can assign to any API Key
// Features = [
// "Paid",
// "Tracking",
// ],
// Optional: Available Features Admin Users can assign to any API Key
// Scopes = [
// "todo:read",
// "todo:write",
// ],
});
})
.ConfigureAppHost(appHost =>
{
using var db = appHost.Resolve<IDbConnectionFactory>().Open();
var feature = appHost.GetPlugin<ApiKeysFeature>();
feature.InitSchema(db);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment