11#include " pch.h"
22#include " combaseapi.h"
33#include < thread>
4+ #include " main.h"
45
56using namespace winrt ;
67using namespace Windows ::Foundation;
@@ -17,20 +18,45 @@ extern "C" __declspec(dllexport) int __stdcall GetWUToken(wchar_t** retToken) {
1718 wprintf (L" Account count = %i\n " , accounts.Size ());
1819 if (accounts.Size () == 0 )
1920 return WU_NO_ACCOUNT;
20- auto accountInfo = accounts.GetAt (0 );
21- wprintf (L" ID = %s\n " , accountInfo.Id ().c_str ());
22- wprintf (L" Name = %s\n " , accountInfo.UserName ().c_str ());
2321
22+ hstring tokenBase64;
23+
24+ // Loop the accounts on the system incase
25+ // the first doesn't have access to the token
26+ for (size_t i = 0 ; i < accounts.Size (); i++)
27+ {
28+ auto accountInfo = accounts.GetAt (i);
29+ wprintf (L" ID = %s\n " , accountInfo.Id ().c_str ());
30+ wprintf (L" Name = %s\n " , accountInfo.UserName ().c_str ());
31+
32+ try {
33+ TryGetToken (accountInfo, tokenBase64);
34+ }
35+ catch (...) {
36+ if (i == accounts.Size () - 1 ) {
37+ throw std::current_exception ();
38+ }
39+ }
40+
41+ if (tokenBase64.size () >= 1 ) {
42+ break ;
43+ }
44+ }
45+
46+ *retToken = (wchar_t *)::CoTaskMemAlloc ((tokenBase64.size () + 1 ) * sizeof (wchar_t ));
47+ memcpy (*retToken, tokenBase64.data (), (tokenBase64.size () + 1 ) * sizeof (wchar_t ));
48+
49+ return S_OK;
50+ }
51+
52+ void TryGetToken (winrt::Windows::Security::Credentials::WebAccount& accountInfo, winrt::hstring& tokenBase64)
53+ {
2454 auto accountProvider = WebAuthenticationCoreManager::FindAccountProviderAsync (L" https://login.microsoft.com" , L" consumers" ).get ();
2555 WebTokenRequest request (accountProvider, L" service::dcat.update.microsoft.com::MBI_SSL" , L" {28520974-CE92-4F36-A219-3F255AF7E61E}" );
2656 auto result = WebAuthenticationCoreManager::GetTokenSilentlyAsync (request, accountInfo).get ();
2757 auto token = result.ResponseData ().GetAt (0 ).Token ();
2858 wprintf (L" Token = %s\n " , token.c_str ());
2959 auto tokenBinary = CryptographicBuffer::ConvertStringToBinary (token, BinaryStringEncoding::Utf16LE);
30- auto tokenBase64 = CryptographicBuffer::EncodeToBase64String (tokenBinary);
60+ tokenBase64 = CryptographicBuffer::EncodeToBase64String (tokenBinary);
3161 wprintf (L" Encoded token = %s\n " , tokenBase64.c_str ());
32-
33- *retToken = (wchar_t *)::CoTaskMemAlloc ((tokenBase64.size () + 1 ) * sizeof (wchar_t ));
34- memcpy (*retToken, tokenBase64.data (), (tokenBase64.size () + 1 ) * sizeof (wchar_t ));
35- return S_OK;
3662}
0 commit comments