@@ -592,38 +592,38 @@ public async Task CanExternalSignIn(bool isPersistent, bool supportsLockout)
592
592
[ InlineData ( true , false ) ]
593
593
[ InlineData ( false , true ) ]
594
594
[ InlineData ( false , false ) ]
595
- public async Task CanResignIn (
596
- // Suppress warning that says theory methods should use all of their parameters.
597
- // See comments below about why this isn't used.
598
- #pragma warning disable xUnit1026
599
- bool isPersistent ,
600
- #pragma warning restore xUnit1026
601
- bool externalLogin )
595
+ public async Task CanResignIn ( bool isPersistent , bool externalLogin )
602
596
{
603
597
// Setup
604
598
var user = new PocoUser { UserName = "Foo" } ;
605
599
var context = new DefaultHttpContext ( ) ;
606
600
var auth = MockAuth ( context ) ;
607
601
var loginProvider = "loginprovider" ;
608
- var id = new ClaimsIdentity ( ) ;
602
+ var id = new ClaimsIdentity ( "authscheme" ) ;
609
603
if ( externalLogin )
610
604
{
611
605
id . AddClaim ( new Claim ( ClaimTypes . AuthenticationMethod , loginProvider ) ) ;
612
606
}
613
- // REVIEW: auth changes we lost the ability to mock is persistent
614
- //var properties = new AuthenticationProperties { IsPersistent = isPersistent };
615
- var authResult = AuthenticateResult . NoResult ( ) ;
607
+
608
+ var claimsPrincipal = new ClaimsPrincipal ( id ) ;
609
+ var properties = new AuthenticationProperties { IsPersistent = isPersistent } ;
610
+ var authResult = AuthenticateResult . Success ( new AuthenticationTicket ( claimsPrincipal , properties , "authscheme" ) ) ;
616
611
auth . Setup ( a => a . AuthenticateAsync ( context , IdentityConstants . ApplicationScheme ) )
617
612
. Returns ( Task . FromResult ( authResult ) ) . Verifiable ( ) ;
618
613
var manager = SetupUserManager ( user ) ;
614
+ manager . Setup ( m => m . GetUserId ( claimsPrincipal ) ) . Returns ( user . Id . ToString ( ) ) ;
619
615
var signInManager = new Mock < SignInManager < PocoUser > > ( manager . Object ,
620
616
new HttpContextAccessor { HttpContext = context } ,
621
617
new Mock < IUserClaimsPrincipalFactory < PocoUser > > ( ) . Object ,
622
618
null , null , new Mock < IAuthenticationSchemeProvider > ( ) . Object , null )
623
619
{ CallBase = true } ;
624
- //signInManager.Setup(s => s.SignInAsync(user, It.Is<AuthenticationProperties>(p => p.IsPersistent == isPersistent),
625
- //externalLogin? loginProvider : null)).Returns(Task.FromResult(0)).Verifiable();
626
- signInManager . Setup ( s => s . SignInWithClaimsAsync ( user , It . IsAny < AuthenticationProperties > ( ) , It . IsAny < IEnumerable < Claim > > ( ) ) ) . Returns ( Task . FromResult ( 0 ) ) . Verifiable ( ) ;
620
+
621
+ signInManager . Setup ( s => s . SignInWithClaimsAsync ( user ,
622
+ It . Is < AuthenticationProperties > ( properties => properties . IsPersistent == isPersistent ) ,
623
+ It . Is < IEnumerable < Claim > > ( claims => ! externalLogin ||
624
+ claims . Any ( claim => claim . Type == ClaimTypes . AuthenticationMethod && claim . Value == loginProvider ) ) ) )
625
+ . Returns ( Task . FromResult ( 0 ) ) . Verifiable ( ) ;
626
+
627
627
signInManager . Object . Context = context ;
628
628
629
629
// Act
@@ -634,6 +634,58 @@ public async Task CanResignIn(
634
634
signInManager . Verify ( ) ;
635
635
}
636
636
637
+ [ Fact ]
638
+ public async Task ResignInNoOpsAndLogsErrorIfNotAuthenticated ( )
639
+ {
640
+ var user = new PocoUser { UserName = "Foo" } ;
641
+ var context = new DefaultHttpContext ( ) ;
642
+ var auth = MockAuth ( context ) ;
643
+ var manager = SetupUserManager ( user ) ;
644
+ var logger = new TestLogger < SignInManager < PocoUser > > ( ) ;
645
+ var signInManager = new Mock < SignInManager < PocoUser > > ( manager . Object ,
646
+ new HttpContextAccessor { HttpContext = context } ,
647
+ new Mock < IUserClaimsPrincipalFactory < PocoUser > > ( ) . Object ,
648
+ null , logger , new Mock < IAuthenticationSchemeProvider > ( ) . Object , null )
649
+ { CallBase = true } ;
650
+ auth . Setup ( a => a . AuthenticateAsync ( context , IdentityConstants . ApplicationScheme ) )
651
+ . Returns ( Task . FromResult ( AuthenticateResult . NoResult ( ) ) ) . Verifiable ( ) ;
652
+
653
+ await signInManager . Object . RefreshSignInAsync ( user ) ;
654
+
655
+ Assert . Contains ( "RefreshSignInAsync prevented because the user is not currently authenticated. Use SignInAsync instead for initial sign in." , logger . LogMessages ) ;
656
+ auth . Verify ( ) ;
657
+ signInManager . Verify ( s => s . SignInWithClaimsAsync ( It . IsAny < PocoUser > ( ) , It . IsAny < AuthenticationProperties > ( ) , It . IsAny < IEnumerable < Claim > > ( ) ) ,
658
+ Times . Never ( ) ) ;
659
+ }
660
+
661
+ [ Fact ]
662
+ public async Task ResignInNoOpsAndLogsErrorIfAuthenticatedWithDifferentUser ( )
663
+ {
664
+ var user = new PocoUser { UserName = "Foo" } ;
665
+ var context = new DefaultHttpContext ( ) ;
666
+ var auth = MockAuth ( context ) ;
667
+ var manager = SetupUserManager ( user ) ;
668
+ var logger = new TestLogger < SignInManager < PocoUser > > ( ) ;
669
+ var signInManager = new Mock < SignInManager < PocoUser > > ( manager . Object ,
670
+ new HttpContextAccessor { HttpContext = context } ,
671
+ new Mock < IUserClaimsPrincipalFactory < PocoUser > > ( ) . Object ,
672
+ null , logger , new Mock < IAuthenticationSchemeProvider > ( ) . Object , null )
673
+ { CallBase = true } ;
674
+ var id = new ClaimsIdentity ( "authscheme" ) ;
675
+ var claimsPrincipal = new ClaimsPrincipal ( id ) ;
676
+ var authResult = AuthenticateResult . Success ( new AuthenticationTicket ( claimsPrincipal , new AuthenticationProperties ( ) , "authscheme" ) ) ;
677
+ auth . Setup ( a => a . AuthenticateAsync ( context , IdentityConstants . ApplicationScheme ) )
678
+ . Returns ( Task . FromResult ( authResult ) ) . Verifiable ( ) ;
679
+ manager . Setup ( m => m . GetUserId ( claimsPrincipal ) ) . Returns ( "different" ) ;
680
+
681
+ await signInManager . Object . RefreshSignInAsync ( user ) ;
682
+
683
+ Assert . Contains ( "RefreshSignInAsync prevented because currently authenticated user has a different UserId. Use SignInAsync instead to change users." , logger . LogMessages ) ;
684
+ auth . Verify ( ) ;
685
+ signInManager . Verify ( s => s . SignInWithClaimsAsync ( It . IsAny < PocoUser > ( ) , It . IsAny < AuthenticationProperties > ( ) , It . IsAny < IEnumerable < Claim > > ( ) ) ,
686
+ Times . Never ( ) ) ;
687
+ }
688
+
637
689
[ Theory ]
638
690
[ InlineData ( true , true , true , true ) ]
639
691
[ InlineData ( true , true , false , true ) ]
0 commit comments