forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScenarioAuthentication.cpp
More file actions
59 lines (52 loc) · 2.45 KB
/
ScenarioAuthentication.cpp
File metadata and controls
59 lines (52 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "ScenarioAuthentication.h"
#include "AppWindow.h"
#include "CheckFailure.h"
using namespace Microsoft::WRL;
ScenarioAuthentication::ScenarioAuthentication(AppWindow* appWindow) : m_appWindow(appWindow)
{
MessageBox(
nullptr,
L"Authentication scenario:\n Click HTML/NTLM Auth to get Authentication headers",
nullptr, MB_OK);
//! [WebResourceResponseReceived]
wil::com_ptr<ICoreWebView2_2> webview2;
CHECK_FAILURE(m_appWindow->GetWebView()->QueryInterface(IID_PPV_ARGS(&webview2)));
CHECK_FAILURE(webview2->add_WebResourceResponseReceived(
Callback<ICoreWebView2WebResourceResponseReceivedEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2WebResourceResponseReceivedEventArgs* args) {
wil::com_ptr<ICoreWebView2WebResourceRequest> request;
CHECK_FAILURE(args->get_Request(&request));
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(request->get_Uri(&uri));
if (wcscmp(uri.get(), L"https://authenticationtest.com/HTTPAuth/") == 0)
{
wil::com_ptr<ICoreWebView2HttpRequestHeaders> requestHeaders;
CHECK_FAILURE(request->get_Headers(&requestHeaders));
wil::unique_cotaskmem_string authHeaderValue;
if (requestHeaders->GetHeader(L"Authorization", &authHeaderValue) == S_OK)
{
std::wstring message(L"Authorization: ");
message += authHeaderValue.get();
MessageBox(nullptr, message.c_str(), nullptr, MB_OK);
m_appWindow->DeleteComponent(this);
}
}
return S_OK;
})
.Get(),
&m_webResourceResponseReceivedToken));
//! [WebResourceResponseReceived]
CHECK_FAILURE(m_appWindow->GetWebView()->Navigate(L"https://authenticationtest.com"));
}
ScenarioAuthentication::~ScenarioAuthentication() {
wil::com_ptr<ICoreWebView2_2> webview2;
CHECK_FAILURE(m_appWindow->GetWebView()->QueryInterface(IID_PPV_ARGS(&webview2)));
CHECK_FAILURE(
webview2->remove_WebResourceResponseReceived(m_webResourceResponseReceivedToken));
}