1+ using MyApp . Data ;
2+ using ServiceStack ;
3+ using ServiceStack . Data ;
4+ using ServiceStack . OrmLite ;
5+ using ServiceStack . Configuration ;
6+
7+ [ assembly: HostingStartup ( typeof ( MyApp . ConfigureApiKeys ) ) ]
8+
9+ namespace MyApp ;
10+
11+ public class ConfigureApiKeys : IHostingStartup
12+ {
13+ public void Configure ( IWebHostBuilder builder ) => builder
14+ . ConfigureServices ( services =>
15+ {
16+ services . AddPlugin ( new ApiKeysFeature
17+ {
18+ // Optional: Available Scopes Admin Users can assign to any API Key
19+ // Features = [
20+ // "Paid",
21+ // "Tracking",
22+ // ],
23+ // Optional: Available Features Admin Users can assign to any API Key
24+ // Scopes = [
25+ // "todo:read",
26+ // "todo:write",
27+ // ],
28+
29+ // Optional: Limit available Scopes Users can assign to their own API Keys
30+ // UserScopes = [
31+ // "todo:read",
32+ // ],
33+ // Optional: Limit available Features Users can assign to their own API Keys
34+ // UserFeatures = [
35+ // "Tracking",
36+ // ],
37+ } ) ;
38+ } )
39+ . ConfigureAppHost ( appHost =>
40+ {
41+ using var db = appHost . Resolve < IDbConnectionFactory > ( ) . Open ( ) ;
42+ var feature = appHost . GetPlugin < ApiKeysFeature > ( ) ;
43+ feature . InitSchema ( db ) ;
44+
45+ // Optional: Create API Key for specified Users on Startup
46+ if ( feature . ApiKeyCount ( db ) == 0 && db . TableExists ( IdentityUsers . TableName ) )
47+ {
48+ var createApiKeysFor = new [ ] { "admin@email.com" , "manager@email.com" , "employee@email.com" } ;
49+ var users = IdentityUsers . GetByUserNames ( db , createApiKeysFor ) ;
50+ foreach ( var user in users )
51+ {
52+ List < string > scopes = user . UserName == "admin@email.com"
53+ ? [ RoleNames . Admin ]
54+ : [ ] ;
55+ feature . Insert ( db ,
56+ new ( ) { Name = "Seed API Key" , UserId = user . Id , UserName = user . UserName , Scopes = scopes } ) ;
57+ }
58+ }
59+ } ) ;
60+ }
0 commit comments