Skip to content

Commit b71c54d

Browse files
committed
Add support for multiple system accounts
1 parent ba37130 commit b71c54d

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

WUTokenHelper/WUTokenHelper.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
</Link>
9696
</ItemDefinitionGroup>
9797
<ItemGroup>
98+
<ClInclude Include="main.h" />
9899
<ClInclude Include="pch.h" />
99100
</ItemGroup>
100101
<ItemGroup>

WUTokenHelper/WUTokenHelper.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<ClInclude Include="pch.h">
1919
<Filter>Header Files</Filter>
2020
</ClInclude>
21+
<ClInclude Include="main.h">
22+
<Filter>Header Files</Filter>
23+
</ClInclude>
2124
</ItemGroup>
2225
<ItemGroup>
2326
<ClCompile Include="pch.cpp">

WUTokenHelper/main.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "pch.h"
22
#include "combaseapi.h"
33
#include <thread>
4+
#include "main.h"
45

56
using namespace winrt;
67
using 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
}

WUTokenHelper/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void GetRawToken(winrt::Windows::Security::Credentials::WebAccount& accountInfo, winrt::hstring& tokenBase64);

0 commit comments

Comments
 (0)