@@ -63,7 +63,7 @@ internal static async Task GetUserAsync(string uid)
6363 // [START get_user_by_id]
6464 UserRecord userRecord = await FirebaseAuth . DefaultInstance . GetUserAsync ( uid ) ;
6565 // 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 } " ) ;
6767 // [END get_user_by_id]
6868 }
6969
@@ -72,7 +72,7 @@ internal static async Task GetUserByEmailAsync(string email)
7272 // [START get_user_by_email]
7373 UserRecord userRecord = await FirebaseAuth . DefaultInstance . GetUserByEmailAsync ( email ) ;
7474 // 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 } " ) ;
7676 // [END get_user_by_email]
7777 }
7878
@@ -81,10 +81,64 @@ internal static async Task GetUserByPhoneNumberAsync(string phoneNumber)
8181 // [START get_user_by_phone]
8282 UserRecord userRecord = await FirebaseAuth . DefaultInstance . GetUserByPhoneNumberAsync ( phoneNumber ) ;
8383 // 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 } " ) ;
8585 // [END get_user_by_phone]
8686 }
8787
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+
88142 internal static async Task DeleteUserAsync ( string uid )
89143 {
90144 // [START delete_user]
@@ -93,6 +147,33 @@ internal static async Task DeleteUserAsync(string uid)
93147 // [END delete_user]
94148 }
95149
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+
96177 internal static async Task SetCustomUserClaimsAsync ( string uid )
97178 {
98179 // [START set_custom_user_claims]
@@ -127,5 +208,42 @@ internal static async Task SetCustomUserClaimsAsync(string uid)
127208 Console . WriteLine ( user . CustomClaims [ "admin" ] ) ;
128209 // [END read_custom_user_claims]
129210 }
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+ }
130248 }
131249}
0 commit comments