Repository: microsoft/terminal
File: src/cascadia/TerminalConnection/AzureConnection.cpp (C++)

Code before:
        else
        {
            // if wCloudShellUri contains the word "servicebus", that means the returned socketUri is of the form
            // wss://ccon-prod-westus-aci-03.servicebus.windows.net/cc-AAAA-AAAAAAAA//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
            // we need to change it to:
            // wss://ccon-prod-westus-aci-03.servicebus.windows.net/$hc/cc-AAAA-AAAAAAAA/terminals/aaaaaaaaaaaaaaaaaaaaaa

            const std::wstring_view wSocketUri{ socketUri };

            // get the substring up until the ".net"
            const auto dotNetStart = wSocketUri.find(L".net");
            THROW_HR_IF(E_UNEXPECTED, dotNetStart == std::wstring::npos);
            const auto dotNetEnd = dotNetStart + 4;
            const auto wSocketUriBody = wSocketUri.substr(0, dotNetEnd);

            // get the portion between the ".net" and the "//" (this is the cc-AAAA-AAAAAAAA part)
            const auto lastDoubleSlashPos = wSocketUri.find_last_of(L"//");
            THROW_HR_IF(E_UNEXPECTED, lastDoubleSlashPos == std::wstring::npos);
            const auto wSocketUriMiddle = wSocketUri.substr(dotNetEnd, lastDoubleSlashPos - (dotNetEnd));

            // piece together the final uri, adding in the "$hc" and "terminals" where needed
            finalSocketUri = fmt::format(FMT_COMPILE(L"{}/$hc{}terminals/{}"), wSocketUriBody, wSocketUriMiddle, _terminalID);
        }

        // Return the uri
        return winrt::hstring(finalSocketUri);
    }

    // Method description:
    // - helper function to store the credentials
    // - we store the display name, tenant ID, access/refresh tokens, and token expiry
    void AzureConnection::_StoreCredential()
    {
        WDJ::JsonObject userName;
        userName.SetNamedValue(L"ver", WDJ::JsonValue::CreateNumberValue(CurrentCredentialVersion));
        _packTenant(userName, *_currentTenant);

        WDJ::JsonObject passWord;
        passWord.SetNamedValue(L"accessToken", WDJ::JsonValue::CreateStringValue(_accessToken));
        passWord.SetNamedValue(L"refreshToken", WDJ::JsonValue::CreateStringValue(_refreshToken));
        passWord.SetNamedValue(L"expiry", WDJ::JsonValue::CreateStringValue(std::to_wstring(_expiry)));

        PasswordVault vault;
        PasswordCredential newCredential{ PasswordVaultResourceName, userName.Stringify(), passWord.Stringify() };
        vault.Add(newCredential);
    }

    // Method description:
    // - helper function to remove all stored credentials
    void AzureConnection::_RemoveCredentials()
    {

Review comment (suggestion):
nit: prefer `winrt::hstring{ }` (unless you have very specific needs, use universal construction via `type{}` rather than function-call-cast `type()`)

Code after:
            // if wCloudShellUri contains the word "servicebus", that means the returned socketUri is of the form
            // wss://ccon-prod-westus-aci-03.servicebus.windows.net/cc-AAAA-AAAAAAAA//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
            // we need to change it to:
            // wss://ccon-prod-westus-aci-03.servicebus.windows.net/$hc/cc-AAAA-AAAAAAAA/terminals/aaaaaaaaaaaaaaaaaaaaaa

            const auto socketUri = terminalResponse.GetNamedString(L"socketUri");
            const std::wstring_view wSocketUri{ socketUri };

            // get the substring up until the ".net"
            const auto dotNetStart = wSocketUri.find(L".net");
            THROW_HR_IF(E_UNEXPECTED, dotNetStart == std::wstring::npos);
            const auto dotNetEnd = dotNetStart + 4;
            const auto wSocketUriBody = wSocketUri.substr(0, dotNetEnd);

            // get the portion between the ".net" and the "//" (this is the cc-AAAA-AAAAAAAA part)
            const auto lastDoubleSlashPos = wSocketUri.find_last_of(L"//");
            THROW_HR_IF(E_UNEXPECTED, lastDoubleSlashPos == std::wstring::npos);
            const auto wSocketUriMiddle = wSocketUri.substr(dotNetEnd, lastDoubleSlashPos - (dotNetEnd));

            // piece together the final uri, adding in the "$hc" and "terminals" where needed
            finalSocketUri = fmt::format(FMT_COMPILE(L"{}/$hc{}terminals/{}"), wSocketUriBody, wSocketUriMiddle, _terminalID);
        }

        // Return the uri
        return winrt::hstring{ finalSocketUri };
    }

    // Method description:
    // - helper function to store the credentials
    // - we store the display name, tenant ID, access/refresh tokens, and token expiry
    void AzureConnection::_StoreCredential()
    {
        WDJ::JsonObject userName;
        userName.SetNamedValue(L"ver", WDJ::JsonValue::CreateNumberValue(CurrentCredentialVersion));
        _packTenant(userName, *_currentTenant);

        WDJ::JsonObject passWord;
        passWord.SetNamedValue(L"accessToken", WDJ::JsonValue::CreateStringValue(_accessToken));
        passWord.SetNamedValue(L"refreshToken", WDJ::JsonValue::CreateStringValue(_refreshToken));
        passWord.SetNamedValue(L"expiry", WDJ::JsonValue::CreateStringValue(std::to_wstring(_expiry)));

        PasswordVault vault;
        PasswordCredential newCredential{ PasswordVaultResourceName, userName.Stringify(), passWord.Stringify() };
        vault.Add(newCredential);
    }

    // Method description:
    // - helper function to remove all stored credentials
    void AzureConnection::_RemoveCredentials()
    {
        PasswordVault vault;