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)) { }
2828 [ PublicAPI ]
2929 public void SetBearerToken ( string accessToken ) => Token = GetToken ( accessToken ) ;
3030
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 } ";
3232
3333 protected override ValueTask < Parameter > GetAuthenticationParameter ( string accessToken )
3434 => 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() {
3434 Assert . True ( authParam . Type == ParameterType . HttpHeader ) ;
3535 Assert . Equal ( _expectedAuthHeaderContent , authParam . Value ) ;
3636 }
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+ }
3751
3852 [ Fact ]
3953 public async Task Check_Only_Header_Authorization ( ) {
You can’t perform that action at this time.
0 commit comments