ServiceStack MicrosoftGraphAuthProvider authentication fails when Client Secret contains special characters

We’ve been using the ServiceStack MicrosoftGraphAuthProvider successfully for some time, however a recent update to the Client Secret caused the authentication to stop working for all users. The sign-in attempt fails, and the browser ends up at the URL /#f=Unknown relative to the site root. The Client Secret is in this format.

We noticed that the new Client Secret contained a plus character where the old one did not, which appears to be the cause of the problem. The HTTP POST request which attempts to acquire an access token is URL encoding the redirect_uri parameter, but does not appear to be encoding the client_secret parameter. We were able to work around the issue by manually URL encoding the new secret in our application configuration (replacing the ‘+’ with ‘%2b’), at which point users could sign-in successfully.

Is there any way to configure the MicrosoftGraphAuthProvider so that it URL encodes the client_secret parameter?


Edit : I’ve found where the token is being requested in the source code and in doing so, have answered my original question – it can’t be configured to URL encode the secret.

From the MicrosoftGraphAuthProvider source code :

    protected override async Task<string> GetAccessTokenJsonAsync
        (string code, AuthContext ctx, CancellationToken token = default)
    {
        var accessTokenParams =
            $"code={code}&client_id={ConsumerKey}&client_secret={ConsumerSecret}&redirect_uri={this.CallbackUrl.UrlEncode()}&grant_type=authorization_code";
        var contents = await AccessTokenUrlFilter(ctx, AccessTokenUrl)
            .SendStringToUrlAsync(method:HttpMethods.Post, requestBody:accessTokenParams, contentType: MimeTypes.FormUrlEncoded, token: token).ConfigAwait();
        return contents;
    }

5

The problem you’re experiencing is due to the client_secret not being URL-encoded when included in the token request. Special characters like + need to be properly encoded as %2B to ensure the request is processed correctly by Microsoft Graph.

To resolve this, you can manually encode the client_secret before passing it into the MicrosoftGraphAuthProvider. Here’s how you can do it:

var encodedSecret = Uri.EscapeDataString(appSettings.GetString("oauth.microsoftgraph.AppSecret"));
authProvider.AppSecret = encodedSecret;

This ensures that any special characters in the client_secret are properly encoded, and you won’t run into issues caused by characters like +.

In your case, you can make use of below modified code as you mentioned in question edit:

From the MicrosoftGraphAuthProvider source code :

protected override async Task<string> GetAccessTokenJsonAsync
    (string code, AuthContext ctx, CancellationToken token = default)
{
    var accessTokenParams = 
        $"code={code}&client_id={ConsumerKey}&client_secret={ConsumerSecret.UrlEncode()}&redirect_uri={this.CallbackUrl.UrlEncode()}&grant_type=authorization_code";

    var contents = await AccessTokenUrlFilter(ctx, AccessTokenUrl)
        .SendStringToUrlAsync(
            method: HttpMethods.Post,
            requestBody: accessTokenParams,
            contentType: MimeTypes.FormUrlEncoded,
            token: token
        ).ConfigAwait();

    return contents;
}

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

ServiceStack MicrosoftGraphAuthProvider authentication fails when Client Secret contains special characters

We’ve been using the ServiceStack MicrosoftGraphAuthProvider successfully for some time, however a recent update to the Client Secret caused the authentication to stop working for all users. The sign-in attempt fails, and the browser ends up at the URL /#f=Unknown relative to the site root. The Client Secret is in this format.

We noticed that the new Client Secret contained a plus character where the old one did not, which appears to be the cause of the problem. The HTTP POST request which attempts to acquire an access token is URL encoding the redirect_uri parameter, but does not appear to be encoding the client_secret parameter. We were able to work around the issue by manually URL encoding the new secret in our application configuration (replacing the ‘+’ with ‘%2b’), at which point users could sign-in successfully.

Is there any way to configure the MicrosoftGraphAuthProvider so that it URL encodes the client_secret parameter?


Edit : I’ve found where the token is being requested in the source code and in doing so, have answered my original question – it can’t be configured to URL encode the secret.

From the MicrosoftGraphAuthProvider source code :

    protected override async Task<string> GetAccessTokenJsonAsync
        (string code, AuthContext ctx, CancellationToken token = default)
    {
        var accessTokenParams =
            $"code={code}&client_id={ConsumerKey}&client_secret={ConsumerSecret}&redirect_uri={this.CallbackUrl.UrlEncode()}&grant_type=authorization_code";
        var contents = await AccessTokenUrlFilter(ctx, AccessTokenUrl)
            .SendStringToUrlAsync(method:HttpMethods.Post, requestBody:accessTokenParams, contentType: MimeTypes.FormUrlEncoded, token: token).ConfigAwait();
        return contents;
    }

5

The problem you’re experiencing is due to the client_secret not being URL-encoded when included in the token request. Special characters like + need to be properly encoded as %2B to ensure the request is processed correctly by Microsoft Graph.

To resolve this, you can manually encode the client_secret before passing it into the MicrosoftGraphAuthProvider. Here’s how you can do it:

var encodedSecret = Uri.EscapeDataString(appSettings.GetString("oauth.microsoftgraph.AppSecret"));
authProvider.AppSecret = encodedSecret;

This ensures that any special characters in the client_secret are properly encoded, and you won’t run into issues caused by characters like +.

In your case, you can make use of below modified code as you mentioned in question edit:

From the MicrosoftGraphAuthProvider source code :

protected override async Task<string> GetAccessTokenJsonAsync
    (string code, AuthContext ctx, CancellationToken token = default)
{
    var accessTokenParams = 
        $"code={code}&client_id={ConsumerKey}&client_secret={ConsumerSecret.UrlEncode()}&redirect_uri={this.CallbackUrl.UrlEncode()}&grant_type=authorization_code";

    var contents = await AccessTokenUrlFilter(ctx, AccessTokenUrl)
        .SendStringToUrlAsync(
            method: HttpMethods.Post,
            requestBody: accessTokenParams,
            contentType: MimeTypes.FormUrlEncoded,
            token: token
        ).ConfigAwait();

    return contents;
}

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật