-
Notifications
You must be signed in to change notification settings - Fork 131
/
FirebaseAuthSnippets.cs
1015 lines (914 loc) · 39 KB
/
FirebaseAuthSnippets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018, Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FirebaseAdmin.Auth;
using FirebaseAdmin.Auth.Hash;
using FirebaseAdmin.Auth.Providers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace FirebaseAdmin.Snippets
{
internal class FirebaseAuthSnippets
{
internal static async Task CreateCustomTokenAsync()
{
// [START custom_token]
var uid = "some-uid";
string customToken = await FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(uid);
// Send token back to client
// [END custom_token]
Console.WriteLine("Created custom token: {0}", customToken);
}
internal static async Task CreateCustomTokenWithClaimsAsync()
{
// [START custom_token_with_claims]
var uid = "some-uid";
var additionalClaims = new Dictionary<string, object>()
{
{ "premiumAccount", true },
};
string customToken = await FirebaseAuth.DefaultInstance
.CreateCustomTokenAsync(uid, additionalClaims);
// Send token back to client
// [END custom_token_with_claims]
Console.WriteLine("Created custom token: {0}", customToken);
}
internal static async Task VeridyIdTokenAsync(string idToken)
{
// [START verify_id_token]
FirebaseToken decodedToken = await FirebaseAuth.DefaultInstance
.VerifyIdTokenAsync(idToken);
string uid = decodedToken.Uid;
// [END verify_id_token]
Console.WriteLine("Decoded ID token from user: {0}", uid);
}
internal static async Task GetUserAsync(string uid)
{
// [START get_user_by_id]
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
// [END get_user_by_id]
}
internal static async Task GetUserByEmailAsync(string email)
{
// [START get_user_by_email]
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserByEmailAsync(email);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
// [END get_user_by_email]
}
internal static async Task GetUserByPhoneNumberAsync(string phoneNumber)
{
// [START get_user_by_phone]
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserByPhoneNumberAsync(phoneNumber);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully fetched user data: {userRecord.Uid}");
// [END get_user_by_phone]
}
internal static async Task GetUsersAsync()
{
// [START bulk_get_users]
GetUsersResult result = await FirebaseAuth.DefaultInstance.GetUsersAsync(
new List<UserIdentifier>
{
new UidIdentifier("uid1"),
new EmailIdentifier("user2@example.com"),
new PhoneIdentifier("+15555550003"),
new ProviderIdentifier("google.com", "google_uid4"),
});
Console.WriteLine("Successfully fetched user data:");
foreach (UserRecord user in result.Users)
{
Console.WriteLine($"User: {user.Uid}");
}
Console.WriteLine("Unable to find users corresponding to these identifiers:");
foreach (UserIdentifier uid in result.NotFound)
{
Console.WriteLine($"{uid}");
}
// [END bulk_get_users]
}
internal static async Task CreateUserAsync()
{
// [START create_user]
UserRecordArgs args = new UserRecordArgs()
{
Email = "user@example.com",
EmailVerified = false,
PhoneNumber = "+11234567890",
Password = "secretPassword",
DisplayName = "John Doe",
PhotoUrl = "http://www.example.com/12345678/photo.png",
Disabled = false,
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.CreateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully created new user: {userRecord.Uid}");
// [END create_user]
}
internal static async Task CreateUserWithUidAsync()
{
// [START create_user_with_uid]
UserRecordArgs args = new UserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PhoneNumber = "+11234567890",
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.CreateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully created new user: {userRecord.Uid}");
// [END create_user_with_uid]
}
internal static async Task ImportUsers()
{
// [START build_user_list]
// Up to 1000 users can be imported at once.
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "uid1",
Email = "user1@example.com",
PasswordHash = Encoding.ASCII.GetBytes("passwordHash1"),
PasswordSalt = Encoding.ASCII.GetBytes("salt1"),
},
new ImportUserRecordArgs()
{
Uid = "uid2",
Email = "user2@example.com",
PasswordHash = Encoding.ASCII.GetBytes("passwordHash2"),
PasswordSalt = Encoding.ASCII.GetBytes("salt2"),
},
};
// [END build_user_list]
// [START import_users]
var options = new UserImportOptions()
{
Hash = new HmacSha256()
{
Key = Encoding.ASCII.GetBytes("secretKey"),
},
};
try
{
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
Console.WriteLine($"Successfully imported {result.SuccessCount} users");
Console.WriteLine($"Failed to import {result.FailureCount} users");
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user at index: {indexedError.Index}"
+ $" due to error: {indexedError.Reason}");
}
}
catch (FirebaseAuthException)
{
// Some unrecoverable error occurred that prevented the operation from running.
}
// [END import_users]
}
internal static async Task ImportWithHmac()
{
// [START import_with_hmac]
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
Hash = new HmacSha256()
{
Key = Encoding.ASCII.GetBytes("secret"),
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
// [END import_with_hmac]
}
internal static async Task ImportWithPbkdf()
{
// [START import_with_pbkdf]
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
Hash = new Pbkdf2Sha256()
{
Rounds = 100000,
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
// [END import_with_pbkdf]
}
internal static async Task ImportWithStandardScrypt()
{
// [START import_with_standard_scrypt]
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
Hash = new StandardScrypt()
{
MemoryCost = 1024,
Parallelization = 16,
BlockSize = 8,
DerivedKeyLength = 64,
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
// [END import_with_standard_scrypt]
}
internal static async Task ImportWithBcrypt()
{
// [START import_with_bcrypt]
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
Hash = new Bcrypt(),
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
// [END import_with_bcrypt]
}
internal static async Task ImportWithScrypt()
{
// [START import_with_scrypt]
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
// All the parameters below can be obtained from the Firebase Console's "Users"
// section. Base64 encoded parameters must be decoded into raw bytes.
Hash = new Scrypt()
{
Key = Encoding.ASCII.GetBytes("base64-secret"),
SaltSeparator = Encoding.ASCII.GetBytes("base64-salt-separator"),
Rounds = 8,
MemoryCost = 14,
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
// [END import_with_scrypt]
}
internal static async Task ImportWithoutPassword()
{
// [START import_without_password]
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
DisplayName = "John Doe",
Email = "johndoe@gmail.com",
PhotoUrl = "http://www.example.com/12345678/photo.png",
EmailVerified = true,
PhoneNumber = "+11234567890",
CustomClaims = new Dictionary<string, object>()
{
{ "admin", true }, // set this user as admin
},
UserProviders = new List<UserProvider>
{
new UserProvider() // user with Google provider
{
Uid = "google-uid",
Email = "johndoe@gmail.com",
DisplayName = "John Doe",
PhotoUrl = "http://www.example.com/12345678/photo.png",
ProviderId = "google.com",
},
},
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
// [END import_without_password]
}
internal static async Task UpdateUserAsync(string uid)
{
// [START update_user]
UserRecordArgs args = new UserRecordArgs()
{
Uid = uid,
Email = "modifiedUser@example.com",
PhoneNumber = "+11234567890",
EmailVerified = true,
Password = "newPassword",
DisplayName = "Jane Doe",
PhotoUrl = "http://www.example.com/12345678/photo.png",
Disabled = true,
};
UserRecord userRecord = await FirebaseAuth.DefaultInstance.UpdateUserAsync(args);
// See the UserRecord reference doc for the contents of userRecord.
Console.WriteLine($"Successfully updated user: {userRecord.Uid}");
// [END update_user]
}
internal static async Task DeleteUserAsync(string uid)
{
// [START delete_user]
await FirebaseAuth.DefaultInstance.DeleteUserAsync(uid);
Console.WriteLine("Successfully deleted user.");
// [END delete_user]
}
internal static async Task DeleteUsersAsync()
{
// [START bulk_delete_users]
DeleteUsersResult result = await FirebaseAuth.DefaultInstance.DeleteUsersAsync(new List<string>
{
"uid1",
"uid2",
"uid3",
});
Console.WriteLine($"Successfully deleted {result.SuccessCount} users.");
Console.WriteLine($"Failed to delete {result.FailureCount} users.");
foreach (ErrorInfo err in result.Errors)
{
Console.WriteLine($"Error #{err.Index}, reason: {err.Reason}");
}
// [END bulk_delete_users]
}
internal static async Task ListAllUsersAsync()
{
// [START list_all_users]
// Start listing users from the beginning, 1000 at a time.
var pagedEnumerable = FirebaseAuth.DefaultInstance.ListUsersAsync(null);
var responses = pagedEnumerable.AsRawResponses().GetAsyncEnumerator();
while (await responses.MoveNextAsync())
{
ExportedUserRecords response = responses.Current;
foreach (ExportedUserRecord user in response.Users)
{
Console.WriteLine($"User: {user.Uid}");
}
}
// Iterate through all users. This will still retrieve users in batches,
// buffering no more than 1000 users in memory at a time.
var enumerator = FirebaseAuth.DefaultInstance.ListUsersAsync(null).GetAsyncEnumerator();
while (await enumerator.MoveNextAsync())
{
ExportedUserRecord user = enumerator.Current;
Console.WriteLine($"User: {user.Uid}");
}
// [END list_all_users]
}
internal static async Task SetCustomUserClaimsAsync(string uid)
{
// [START set_custom_user_claims]
// Set admin privileges on the user corresponding to uid.
var claims = new Dictionary<string, object>()
{
{ "admin", true },
};
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(uid, claims);
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
// [END set_custom_user_claims]
var idToken = "id_token";
// [START verify_custom_claims]
// Verify the ID token first.
FirebaseToken decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
object isAdmin;
if (decoded.Claims.TryGetValue("admin", out isAdmin))
{
if ((bool)isAdmin)
{
// Allow access to requested admin resource.
}
}
// [END verify_custom_claims]
// [START read_custom_user_claims]
// Lookup the user associated with the specified uid.
UserRecord user = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
Console.WriteLine(user.CustomClaims["admin"]);
// [END read_custom_user_claims]
}
internal static async Task SetCustomUserClaimsScriptAsync()
{
// [START set_custom_user_claims_script]
UserRecord user = await FirebaseAuth.DefaultInstance
.GetUserByEmailAsync("user@admin.example.com");
// Confirm user is verified.
if (user.EmailVerified)
{
var claims = new Dictionary<string, object>()
{
{ "admin", true },
};
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
}
// [END set_custom_user_claims_script]
}
internal static async Task SetCustomUserClaimsIncrementalAsync()
{
// [START set_custom_user_claims_incremental]
UserRecord user = await FirebaseAuth.DefaultInstance
.GetUserByEmailAsync("user@admin.example.com");
// Add incremental custom claims without overwriting the existing claims.
object isAdmin;
if (user.CustomClaims.TryGetValue("admin", out isAdmin) && (bool)isAdmin)
{
var claims = new Dictionary<string, object>(user.CustomClaims);
// Add level.
claims["level"] = 10;
// Add custom claims for additional privileges.
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
}
// [END set_custom_user_claims_incremental]
}
internal static async Task RevokeIdTokens(string idToken)
{
string uid = "someUid";
// [START revoke_tokens]
await FirebaseAuth.DefaultInstance.RevokeRefreshTokensAsync(uid);
var user = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
Console.WriteLine("Tokens revoked at: " + user.TokensValidAfterTimestamp);
// [END revoke_tokens]
}
internal static async Task VerifyIdTokenCheckRevoked(string idToken)
{
// [START verify_id_token_check_revoked]
try
{
// Verify the ID token while checking if the token is revoked by passing checkRevoked
// as true.
bool checkRevoked = true;
var decodedToken = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(
idToken, checkRevoked);
// Token is valid and not revoked.
string uid = decodedToken.Uid;
}
catch (FirebaseAuthException ex)
{
if (ex.AuthErrorCode == AuthErrorCode.RevokedIdToken)
{
// Token has been revoked. Inform the user to re-authenticate or signOut() the user.
}
else
{
// Token is invalid.
}
}
// [END verify_id_token_check_revoked]
}
internal static ActionCodeSettings InitActionCodeSettings()
{
// [START init_action_code_settings]
var actionCodeSettings = new ActionCodeSettings()
{
Url = "https://www.example.com/checkout?cartId=1234",
HandleCodeInApp = true,
IosBundleId = "com.example.ios",
AndroidPackageName = "com.example.android",
AndroidInstallApp = true,
AndroidMinimumVersion = "12",
DynamicLinkDomain = "coolapp.page.link",
};
// [END init_action_code_settings]
return actionCodeSettings;
}
internal static async Task GeneratePasswordResetLink()
{
var actionCodeSettings = InitActionCodeSettings();
var displayName = "Example User";
// [START password_reset_link]
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GeneratePasswordResetLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
// [END password_reset_link]
}
internal static async Task GenerateEmailVerificationLink()
{
var actionCodeSettings = InitActionCodeSettings();
var displayName = "Example User";
// [START email_verification_link]
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateEmailVerificationLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
// [END email_verification_link]
}
internal static async Task GenerateSignInWithEmailLink()
{
var actionCodeSettings = InitActionCodeSettings();
var displayName = "Example User";
// [START sign_in_with_email_link]
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateSignInWithEmailLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
// [END sign_in_with_email_link]
}
internal static async Task CreateSamlProviderConfig()
{
// [START create_saml_provider]
var args = new SamlProviderConfigArgs()
{
DisplayName = "SAML provider name",
Enabled = true,
ProviderId = "saml.myProvider",
IdpEntityId = "IDP_ENTITY_ID",
SsoUrl = "https://example.com/saml/sso/1234/",
X509Certificates = new List<string>()
{
"-----BEGIN CERTIFICATE-----\nCERT1...\n-----END CERTIFICATE-----",
"-----BEGIN CERTIFICATE-----\nCERT2...\n-----END CERTIFICATE-----",
},
RpEntityId = "RP_ENTITY_ID",
CallbackUrl = "https://project-id.firebaseapp.com/__/auth/handler",
};
SamlProviderConfig saml = await FirebaseAuth.DefaultInstance
.CreateProviderConfigAsync(args);
Console.WriteLine($"Created new SAML provider: {saml.ProviderId}");
// [END create_saml_provider]
}
internal static async Task UpdateSamlProviderConfig()
{
// [START update_saml_provider]
var args = new SamlProviderConfigArgs()
{
ProviderId = "saml.myProvider",
X509Certificates = new List<string>()
{
"-----BEGIN CERTIFICATE-----\nCERT2...\n-----END CERTIFICATE-----",
"-----BEGIN CERTIFICATE-----\nCERT3...\n-----END CERTIFICATE-----",
},
};
SamlProviderConfig saml = await FirebaseAuth.DefaultInstance
.UpdateProviderConfigAsync(args);
Console.WriteLine($"Updated SAML provider: {saml.ProviderId}");
// [END update_saml_provider]
}
internal static async Task GetSamlProviderConfig()
{
// [START get_saml_provider]
SamlProviderConfig saml = await FirebaseAuth.DefaultInstance
.GetSamlProviderConfigAsync("saml.myProvider");
Console.WriteLine($"{saml.DisplayName}: {saml.Enabled}");
// [END get_saml_provider]
}
internal static async Task DeleteSamlProviderConfig()
{
// [START delete_saml_provider]
await FirebaseAuth.DefaultInstance.DeleteProviderConfigAsync("saml.myProvider");
// [END delete_saml_provider]
}
internal static async Task ListSamlProviderConfigs()
{
// [START list_saml_providers]
var listOptions = new ListProviderConfigsOptions()
{
PageToken = "nextPageToken",
};
IAsyncEnumerator<SamlProviderConfig> enumerator = FirebaseAuth.DefaultInstance
.ListSamlProviderConfigsAsync(listOptions)
.GetAsyncEnumerator();
while (await enumerator.MoveNextAsync())
{
SamlProviderConfig saml = enumerator.Current;
Console.WriteLine(saml.ProviderId);
}
// [END list_saml_providers]
}
internal static async Task CreateOidcProviderConfig()
{
// [START create_oidc_provider]
var args = new OidcProviderConfigArgs()
{
DisplayName = "OIDC provider name",
Enabled = true,
ProviderId = "oidc.myProvider",
ClientId = "CLIENT_ID2",
Issuer = "https://oidc.com/CLIENT_ID2",
};
OidcProviderConfig oidc = await FirebaseAuth.DefaultInstance
.CreateProviderConfigAsync(args);
Console.WriteLine($"Created new OIDC provider: {oidc.ProviderId}");
// [END create_oidc_provider]
}
internal static async Task UpdateOidcProviderConfig()
{
// [START update_oidc_provider]
var args = new OidcProviderConfigArgs()
{
DisplayName = "OIDC provider name",
Enabled = true,
ProviderId = "oidc.myProvider",
ClientId = "CLIENT_ID",
Issuer = "https://oidc.com",
};
OidcProviderConfig oidc = await FirebaseAuth.DefaultInstance
.UpdateProviderConfigAsync(args);
Console.WriteLine($"Updated OIDC provider: {oidc.ProviderId}");
// [END update_oidc_provider]
}
internal static async Task GetOidcProviderConfig()
{
// [START get_oidc_provider]
OidcProviderConfig oidc = await FirebaseAuth.DefaultInstance
.GetOidcProviderConfigAsync("oidc.myProvider");
Console.WriteLine($"{oidc.DisplayName}: {oidc.Enabled}");
// [END get_oidc_provider]
}
internal static async Task DeleteOidcProviderConfig()
{
// [START delete_oidc_provider]
await FirebaseAuth.DefaultInstance.DeleteProviderConfigAsync("oidc.myProvider");
// [END delete_oidc_provider]
}
internal static async Task ListOidcProviderConfigs()
{
// [START list_oidc_providers]
var listOptions = new ListProviderConfigsOptions()
{
PageToken = "nextPageToken",
};
IAsyncEnumerator<OidcProviderConfig> enumerator = FirebaseAuth.DefaultInstance
.ListOidcProviderConfigsAsync(listOptions)
.GetAsyncEnumerator();
while (await enumerator.MoveNextAsync())
{
OidcProviderConfig oidc = enumerator.Current;
Console.WriteLine(oidc.ProviderId);
}
// [END list_oidc_providers]
}
// Place holder method to make the compiler happy. This is referenced by all email action
// link snippets.
private static void SendCustomEmail(string email, string displayName, string link) { }
public class LoginRequest
{
public string IdToken { get; set; }
}
public class SessionCookieSnippets : ControllerBase
{
// [START session_login]
// POST: /sessionLogin
[HttpPost]
public async Task<ActionResult> Login([FromBody] LoginRequest request)
{
// Set session expiration to 5 days.
var options = new SessionCookieOptions()
{
ExpiresIn = TimeSpan.FromDays(5),
};
try
{
// Create the session cookie. This will also verify the ID token in the process.
// The session cookie will have the same claims as the ID token.
var sessionCookie = await FirebaseAuth.DefaultInstance
.CreateSessionCookieAsync(request.IdToken, options);
// Set cookie policy parameters as required.
var cookieOptions = new CookieOptions()
{
Expires = DateTimeOffset.UtcNow.Add(options.ExpiresIn),
HttpOnly = true,
Secure = true,
};
this.Response.Cookies.Append("session", sessionCookie, cookieOptions);
return this.Ok();
}
catch (FirebaseAuthException)
{
return this.Unauthorized("Failed to create a session cookie");
}
}
// [END session_login]
// [START session_verify]
// POST: /profile
[HttpPost]
public async Task<ActionResult> Profile()
{
var sessionCookie = this.Request.Cookies["session"];
if (string.IsNullOrEmpty(sessionCookie))
{
// Session cookie is not available. Force user to login.
return this.Redirect("/login");
}
try
{
// Verify the session cookie. In this case an additional check is added to detect
// if the user's Firebase session was revoked, user deleted/disabled, etc.
var checkRevoked = true;
var decodedToken = await FirebaseAuth.DefaultInstance.VerifySessionCookieAsync(
sessionCookie, checkRevoked);
return ViewContentForUser(decodedToken);
}
catch (FirebaseAuthException)
{
// Session cookie is invalid or revoked. Force user to login.
return this.Redirect("/login");
}
}
// [END session_verify]
// [START session_clear]
// POST: /sessionLogout
[HttpPost]
public ActionResult ClearSessionCookie()
{
this.Response.Cookies.Delete("session");
return this.Redirect("/login");
}
// [END session_clear]
// [START session_clear_and_revoke]
// POST: /sessionLogout
[HttpPost]
public async Task<ActionResult> ClearSessionCookieAndRevoke()
{
var sessionCookie = this.Request.Cookies["session"];
try
{
var decodedToken = await FirebaseAuth.DefaultInstance
.VerifySessionCookieAsync(sessionCookie);
await FirebaseAuth.DefaultInstance.RevokeRefreshTokensAsync(decodedToken.Uid);
this.Response.Cookies.Delete("session");
return this.Redirect("/login");
}
catch (FirebaseAuthException)
{
return this.Redirect("/login");
}
}
// [END session_clear_and_revoke]
internal async Task<ActionResult> CheckAuthTime(string idToken)
{
// [START check_auth_time]
// To ensure that cookies are set only on recently signed in users, check auth_time in
// ID token before creating a cookie.
var decodedToken = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
var authTime = new DateTime(1970, 1, 1).AddSeconds(
(long)decodedToken.Claims["auth_time"]);
// Only process if the user signed in within the last 5 minutes.
if (DateTime.UtcNow - authTime < TimeSpan.FromMinutes(5))
{
var options = new SessionCookieOptions()
{
ExpiresIn = TimeSpan.FromDays(5),
};
var sessionCookie = await FirebaseAuth.DefaultInstance.CreateSessionCookieAsync(
idToken, options);
// Set cookie policy parameters as required.
this.Response.Cookies.Append("session", sessionCookie);
return this.Ok();
}
// User did not sign in recently. To guard against ID token theft, require
// re-authentication.
return this.Unauthorized("Recent sign in required");
// [END check_auth_time]
}
internal async Task<ActionResult> CheckPermissions(string sessionCookie)
{
// [START session_verify_with_permission_check]
try
{
var checkRevoked = true;
var decodedToken = await FirebaseAuth.DefaultInstance.VerifySessionCookieAsync(
sessionCookie, checkRevoked);
object isAdmin;
if (decodedToken.Claims.TryGetValue("admin", out isAdmin) && (bool)isAdmin)
{
return ViewContentForAdmin(decodedToken);
}
return this.Unauthorized("Insufficient permissions");
}
catch (FirebaseAuthException)
{
// Session cookie is invalid or revoked. Force user to login.
return this.Redirect("/login");
}