@@ -63,7 +63,7 @@ internal static async Task GetUserAsync(string uid)
63
63
// [START get_user_by_id]
64
64
UserRecord userRecord = await FirebaseAuth . DefaultInstance . GetUserAsync ( uid ) ;
65
65
// See the UserRecord reference doc for the contents of userRecord.
66
- Console . WriteLine ( "Successfully fetched user data: {0}" , userRecord . Uid ) ;
66
+ Console . WriteLine ( $ "Successfully fetched user data: { userRecord . Uid } " ) ;
67
67
// [END get_user_by_id]
68
68
}
69
69
@@ -72,7 +72,7 @@ internal static async Task GetUserByEmailAsync(string email)
72
72
// [START get_user_by_email]
73
73
UserRecord userRecord = await FirebaseAuth . DefaultInstance . GetUserByEmailAsync ( email ) ;
74
74
// See the UserRecord reference doc for the contents of userRecord.
75
- Console . WriteLine ( "Successfully fetched user data: {0}" , userRecord . Uid ) ;
75
+ Console . WriteLine ( $ "Successfully fetched user data: { userRecord . Uid } " ) ;
76
76
// [END get_user_by_email]
77
77
}
78
78
@@ -81,10 +81,64 @@ internal static async Task GetUserByPhoneNumberAsync(string phoneNumber)
81
81
// [START get_user_by_phone]
82
82
UserRecord userRecord = await FirebaseAuth . DefaultInstance . GetUserByPhoneNumberAsync ( phoneNumber ) ;
83
83
// See the UserRecord reference doc for the contents of userRecord.
84
- Console . WriteLine ( "Successfully fetched user data: {0}" , userRecord . Uid ) ;
84
+ Console . WriteLine ( $ "Successfully fetched user data: { userRecord . Uid } " ) ;
85
85
// [END get_user_by_phone]
86
86
}
87
87
88
+ internal static async Task CreateUserAsync ( )
89
+ {
90
+ // [START create_user]
91
+ UserRecordArgs args = new UserRecordArgs ( )
92
+ {
93
+
94
+ EmailVerified = false ,
95
+ PhoneNumber = "+11234567890" ,
96
+ Password = "secretPassword" ,
97
+ DisplayName = "John Doe" ,
98
+ PhotoUrl = "http://www.example.com/12345678/photo.png" ,
99
+ Disabled = false ,
100
+ } ;
101
+ UserRecord userRecord = await FirebaseAuth . DefaultInstance . CreateUserAsync ( args ) ;
102
+ // See the UserRecord reference doc for the contents of userRecord.
103
+ Console . WriteLine ( $ "Successfully created new user: { userRecord . Uid } ") ;
104
+ // [END create_user]
105
+ }
106
+
107
+ internal static async Task CreateUserWithUidAsync ( )
108
+ {
109
+ // [START create_user_with_uid]
110
+ UserRecordArgs args = new UserRecordArgs ( )
111
+ {
112
+ Uid = "some-uid" ,
113
+
114
+ PhoneNumber = "+11234567890" ,
115
+ } ;
116
+ UserRecord userRecord = await FirebaseAuth . DefaultInstance . CreateUserAsync ( args ) ;
117
+ // See the UserRecord reference doc for the contents of userRecord.
118
+ Console . WriteLine ( $ "Successfully created new user: { userRecord . Uid } ") ;
119
+ // [END create_user_with_uid]
120
+ }
121
+
122
+ internal static async Task UpdateUserAsync ( string uid )
123
+ {
124
+ // [START update_user]
125
+ UserRecordArgs args = new UserRecordArgs ( )
126
+ {
127
+ Uid = uid ,
128
+
129
+ PhoneNumber = "+11234567890" ,
130
+ EmailVerified = true ,
131
+ Password = "newPassword" ,
132
+ DisplayName = "Jane Doe" ,
133
+ PhotoUrl = "http://www.example.com/12345678/photo.png" ,
134
+ Disabled = true ,
135
+ } ;
136
+ UserRecord userRecord = await FirebaseAuth . DefaultInstance . UpdateUserAsync ( args ) ;
137
+ // See the UserRecord reference doc for the contents of userRecord.
138
+ Console . WriteLine ( $ "Successfully updated user: { userRecord . Uid } ") ;
139
+ // [END update_user]
140
+ }
141
+
88
142
internal static async Task DeleteUserAsync ( string uid )
89
143
{
90
144
// [START delete_user]
@@ -93,6 +147,33 @@ internal static async Task DeleteUserAsync(string uid)
93
147
// [END delete_user]
94
148
}
95
149
150
+ internal static async Task ListAllUsersAsync ( )
151
+ {
152
+ // [START list_all_users]
153
+ // Start listing users from the beginning, 1000 at a time.
154
+ var pagedEnumerable = FirebaseAuth . DefaultInstance . ListUsersAsync ( null ) ;
155
+ var responses = pagedEnumerable . AsRawResponses ( ) . GetEnumerator ( ) ;
156
+ while ( await responses . MoveNext ( ) )
157
+ {
158
+ ExportedUserRecords response = responses . Current ;
159
+ foreach ( ExportedUserRecord user in response . Users )
160
+ {
161
+ Console . WriteLine ( $ "User: { user . Uid } ") ;
162
+ }
163
+ }
164
+
165
+ // Iterate through all users. This will still retrieve users in batches,
166
+ // buffering no more than 1000 users in memory at a time.
167
+ var enumerator = FirebaseAuth . DefaultInstance . ListUsersAsync ( null ) . GetEnumerator ( ) ;
168
+ while ( await enumerator . MoveNext ( ) )
169
+ {
170
+ ExportedUserRecord user = enumerator . Current ;
171
+ Console . WriteLine ( $ "User: { user . Uid } ") ;
172
+ }
173
+
174
+ // [END list_all_users]
175
+ }
176
+
96
177
internal static async Task SetCustomUserClaimsAsync ( string uid )
97
178
{
98
179
// [START set_custom_user_claims]
@@ -127,5 +208,42 @@ internal static async Task SetCustomUserClaimsAsync(string uid)
127
208
Console . WriteLine ( user . CustomClaims [ "admin" ] ) ;
128
209
// [END read_custom_user_claims]
129
210
}
211
+
212
+ internal static async Task SetCustomUserClaimsScriptAsync ( )
213
+ {
214
+ // [START set_custom_user_claims_script]
215
+ UserRecord user = await FirebaseAuth . DefaultInstance
216
+ . GetUserByEmailAsync ( "[email protected] " ) ;
217
+ // Confirm user is verified.
218
+ if ( user . EmailVerified )
219
+ {
220
+ var claims = new Dictionary < string , object > ( )
221
+ {
222
+ { "admin" , true } ,
223
+ } ;
224
+ await FirebaseAuth . DefaultInstance . SetCustomUserClaimsAsync ( user . Uid , claims ) ;
225
+ }
226
+
227
+ // [END set_custom_user_claims_script]
228
+ }
229
+
230
+ internal static async Task SetCustomUserClaimsIncrementalAsync ( )
231
+ {
232
+ // [START set_custom_user_claims_incremental]
233
+ UserRecord user = await FirebaseAuth . DefaultInstance
234
+ . GetUserByEmailAsync ( "[email protected] " ) ;
235
+ // Add incremental custom claims without overwriting the existing claims.
236
+ object isAdmin ;
237
+ if ( user . CustomClaims . TryGetValue ( "admin" , out isAdmin ) && ( bool ) isAdmin )
238
+ {
239
+ var claims = new Dictionary < string , object > ( user . CustomClaims ) ;
240
+ // Add level.
241
+ claims [ "level" ] = 10 ;
242
+ // Add custom claims for additional privileges.
243
+ await FirebaseAuth . DefaultInstance . SetCustomUserClaimsAsync ( user . Uid , claims ) ;
244
+ }
245
+
246
+ // [END set_custom_user_claims_incremental]
247
+ }
130
248
}
131
249
}
0 commit comments