File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed
src/RestSharp/Authenticators Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ public JwtAuthenticator(string accessToken) : base(GetToken(accessToken)) { }
28
28
[ PublicAPI ]
29
29
public void SetBearerToken ( string accessToken ) => Token = GetToken ( accessToken ) ;
30
30
31
- static string GetToken ( string accessToken ) => $ "Bearer { Ensure . NotEmpty ( accessToken , nameof ( accessToken ) ) } ";
31
+ static string GetToken ( string accessToken ) => Ensure . NotEmpty ( accessToken , nameof ( accessToken ) ) . StartsWith ( "Bearer " ) ? accessToken : $ "Bearer { accessToken } ";
32
32
33
33
protected override ValueTask < Parameter > GetAuthenticationParameter ( string accessToken )
34
34
=> new ( new HeaderParameter ( KnownHeaders . Authorization , accessToken ) ) ;
Original file line number Diff line number Diff line change @@ -34,6 +34,20 @@ public async Task Can_Set_ValidFormat_Auth_Header() {
34
34
Assert . True ( authParam . Type == ParameterType . HttpHeader ) ;
35
35
Assert . Equal ( _expectedAuthHeaderContent , authParam . Value ) ;
36
36
}
37
+
38
+ [ Fact ]
39
+ public async Task Can_Set_ValidFormat_Auth_Header_With_Bearer_Prefix ( ) {
40
+ var client = new RestClient { Authenticator = new JwtAuthenticator ( $ "Bearer { _testJwt } ") } ;
41
+ var request = new RestRequest ( ) ;
42
+
43
+ //In real case client.Execute(request) will invoke Authenticate method
44
+ await client . Authenticator . Authenticate ( client , request ) ;
45
+
46
+ var authParam = request . Parameters . Single ( p => p . Name . Equals ( KnownHeaders . Authorization , StringComparison . OrdinalIgnoreCase ) ) ;
47
+
48
+ Assert . True ( authParam . Type == ParameterType . HttpHeader ) ;
49
+ Assert . Equal ( _expectedAuthHeaderContent , authParam . Value ) ;
50
+ }
37
51
38
52
[ Fact ]
39
53
public async Task Check_Only_Header_Authorization ( ) {
You can’t perform that action at this time.
0 commit comments