1
+ // snippet.using
2
+ using PubnubApi ;
3
+
4
+ // snippet.end
5
+
6
+ public class AccessManagerV2Sample
7
+ {
8
+ private static Pubnub pubnub ;
9
+
10
+ static void PubnubInit ( )
11
+ {
12
+ // snippet.pubnub_init
13
+ //Create configuration
14
+ PNConfiguration pnConfiguration = new PNConfiguration ( new UserId ( "myUniqueUserId" ) )
15
+ {
16
+ SubscribeKey = "demo" ,
17
+ PublishKey = "demo"
18
+ } ;
19
+ //Create a new PubNub instance
20
+ Pubnub pubnub = new Pubnub ( pnConfiguration ) ;
21
+
22
+ // snippet.end
23
+ }
24
+
25
+ static async Task Grant ( )
26
+ {
27
+ // snippet.grant
28
+ PNResult < PNAccessManagerGrantResult > grantResponse = await pubnub . Grant ( )
29
+ . Channels ( new string [ ] {
30
+ //channels to allow grant on
31
+ "ch1" ,
32
+ "ch2" ,
33
+ "ch3"
34
+ } )
35
+ . ChannelGroups ( new string [ ] {
36
+ // groups to allow grant on
37
+ "cg1" ,
38
+ "cg2" ,
39
+ "cg3"
40
+ } )
41
+ . AuthKeys ( new string [ ] {
42
+ // the keys we are provisioning
43
+ "key1" ,
44
+ "key2" ,
45
+ "key3"
46
+ } )
47
+ . Write ( true ) // allow those keys to write (false by default)
48
+ . Manage ( true ) // allow those keys to manage channel groups (false by default)
49
+ . Read ( true ) // allow keys to read the subscribe feed (false by default)
50
+ . Delete ( true ) // allow those keys to delete the subscribe feed (false by default)
51
+ . TTL ( 12337 ) // how long those keys will remain valid (0 for eternity)
52
+ . ExecuteAsync ( ) ;
53
+
54
+ PNAccessManagerGrantResult grantResult = grantResponse . Result ;
55
+ PNStatus status = grantResponse . Status ;
56
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
57
+ // snippet.end
58
+ }
59
+
60
+ static void GrantCallback ( )
61
+ {
62
+ // snippet.grant_callback
63
+ pubnub . Grant ( )
64
+ . Channels ( new string [ ] {
65
+ //channels to allow grant on
66
+ "ch1" ,
67
+ "ch2" ,
68
+ "ch3"
69
+ } )
70
+ . ChannelGroups ( new string [ ] {
71
+ // groups to allow grant on
72
+ "cg1" ,
73
+ "cg2" ,
74
+ "cg3"
75
+ } )
76
+ . AuthKeys ( new string [ ] {
77
+ // the keys we are provisioning
78
+ "key1" ,
79
+ "key2" ,
80
+ "key3"
81
+ } )
82
+ . Write ( true ) // allow those keys to write (false by default)
83
+ . Manage ( true ) // allow those keys to manage channel groups (false by default)
84
+ . Read ( true ) // allow keys to read the subscribe feed (false by default)
85
+ . Delete ( true ) // allow those keys to delete the subscribe feed (false by default)
86
+ . TTL ( 12337 ) // how long those keys will remain valid (0 for eternity)
87
+ . Execute ( new PNAccessManagerGrantResultExt (
88
+ ( result , status ) => {
89
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
90
+ }
91
+ ) ) ;
92
+ // snippet.end
93
+ }
94
+
95
+ static async Task GrantTTL ( )
96
+ {
97
+ // snippet.grant_ttl
98
+ PNResult < PNAccessManagerGrantResult > grantResponse = await pubnub . Grant ( )
99
+ . Channels ( new string [ ] {
100
+ "my_channel"
101
+ } )
102
+ . Write ( false )
103
+ . Read ( true )
104
+ . Delete ( false )
105
+ . AuthKeys ( new string [ ] {
106
+ "my_ro_authkey"
107
+ } )
108
+ . TTL ( 5 )
109
+ . ExecuteAsync ( ) ;
110
+
111
+ PNAccessManagerGrantResult grantResult = grantResponse . Result ;
112
+ PNStatus status = grantResponse . Status ;
113
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
114
+ // snippet.end
115
+ }
116
+
117
+ static async Task GrantPresence ( )
118
+ {
119
+ // snippet.grant_presence
120
+ PNResult < PNAccessManagerGrantResult > grantResponse = await pubnub . Grant ( )
121
+ . Channels ( new string [ ] {
122
+ "my_channel-pnpres"
123
+ } )
124
+ . Write ( true )
125
+ . Read ( true )
126
+ . Delete ( true )
127
+ . ExecuteAsync ( ) ;
128
+
129
+ PNAccessManagerGrantResult grantResult = grantResponse . Result ;
130
+ PNStatus status = grantResponse . Status ;
131
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
132
+ // snippet.end
133
+ }
134
+
135
+ static async Task GrantChannelGroup ( )
136
+ {
137
+ // snippet.grant_group
138
+ PNResult < PNAccessManagerGrantResult > grantResponse = await pubnub . Grant ( )
139
+ . ChannelGroups ( new string [ ] {
140
+ "cg1" ,
141
+ "cg2" ,
142
+ "cg3"
143
+ } )
144
+ . AuthKeys ( new string [ ] {
145
+ "key1" ,
146
+ "key2" ,
147
+ "key3"
148
+ } )
149
+ . Write ( true )
150
+ . Manage ( true )
151
+ . Read ( true )
152
+ . Delete ( true )
153
+ . TTL ( 12337 )
154
+ . ExecuteAsync ( ) ;
155
+
156
+ PNAccessManagerGrantResult grantResult = grantResponse . Result ;
157
+ PNStatus status = grantResponse . Status ;
158
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
159
+ // snippet.end
160
+ }
161
+
162
+ static async Task GrantWithAuthKey ( )
163
+ {
164
+ // snippet.grant_auth_key
165
+ PNResult < PNAccessManagerGrantResult > grantResponse = await pubnub . Grant ( )
166
+ . Uuids ( new string [ ] {
167
+ "my_uuid"
168
+ } )
169
+ . Get ( true )
170
+ . Update ( true )
171
+ . Delete ( true )
172
+ . AuthKeys ( new string [ ] {
173
+ "my_ro_authkey"
174
+ } )
175
+ . TTL ( 1440 )
176
+ . ExecuteAsync ( ) ;
177
+
178
+ PNAccessManagerGrantResult grantResult = grantResponse . Result ;
179
+ PNStatus status = grantResponse . Status ;
180
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
181
+ // snippet.end
182
+ }
183
+
184
+ static async Task GrantAppLevel ( )
185
+ {
186
+ // snippet.grant_app_level
187
+ PNResult < PNAccessManagerGrantResult > grantResponse = await pubnub . Grant ( )
188
+ . Write ( true )
189
+ . Read ( true )
190
+ . Delete ( true )
191
+ . ExecuteAsync ( ) ;
192
+
193
+ PNAccessManagerGrantResult grantResult = grantResponse . Result ;
194
+ PNStatus status = grantResponse . Status ;
195
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
196
+ // snippet.end
197
+ }
198
+
199
+ static async Task GrantChannelLevel ( )
200
+ {
201
+ // snippet.grant_channel_level
202
+ PNResult < PNAccessManagerGrantResult > grantResponse = await pubnub . Grant ( )
203
+ . Channels ( new string [ ] {
204
+ "my_channel"
205
+ } )
206
+ . Write ( true )
207
+ . Read ( true )
208
+ . Delete ( true )
209
+ . ExecuteAsync ( ) ;
210
+
211
+ PNAccessManagerGrantResult grantResult = grantResponse . Result ;
212
+ PNStatus status = grantResponse . Status ;
213
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
214
+ // snippet.end
215
+ }
216
+
217
+ static async Task GrantUserLevel ( )
218
+ {
219
+ // snippet.grant_user_level
220
+ PNResult < PNAccessManagerGrantResult > grantResponse = await pubnub . Grant ( )
221
+ . Channels ( new string [ ] {
222
+ "my_channel"
223
+ } )
224
+ . Write ( true )
225
+ . Read ( true )
226
+ . Delete ( true )
227
+ . AuthKeys ( new string [ ] {
228
+ "my_authkey"
229
+ } )
230
+ . TTL ( 5 )
231
+ . ExecuteAsync ( ) ;
232
+
233
+ PNAccessManagerGrantResult grantResult = grantResponse . Result ;
234
+ PNStatus status = grantResponse . Status ;
235
+ //PNAccessManagerGrantResult is a parsed and abstracted response from server
236
+ // snippet.end
237
+ }
238
+ }
0 commit comments