Skip to content

Commit a2bf7a1

Browse files
author
Çağrı Kaan Yıldırım
committed
Update WebAuthenticationRequested.md
1 parent 8ea4a8a commit a2bf7a1

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

specs/WebAuthenticationRequested.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ We also propose CoreWebView2WebAuthenticationResponse, the runtime class that re
3636
The developer can provide the authentication credentials on behalf of the user when it encounters the Basic authentication request. In this case, the default login dialog prompt will no longer be shown to the user. If the developer provided credentials are wrong, the server may keep responding with Unauthorized, which will lead to an infinite loop so the developer should pay attention to this.
3737

3838
```cpp
39+
3940
webview2->add_WebAuthenticationRequested(
4041
Callback<ICoreWebView2WebAuthenticationRequestedEventHandler>(
4142
[this](
@@ -45,11 +46,17 @@ The developer can provide the authentication credentials on behalf of the user w
4546
wil::com_ptr<ICoreWebView2Environment> webviewEnvironment;
4647
m_appWindow->GetWebViewEnvironment()->QueryInterface(
4748
IID_PPV_ARGS(&webviewEnvironment));
48-
wil::com_ptr<ICoreWebView2WebAuthenticationResponse> webAuthenticationResponse;
49-
webviewEnvironment->CreateWebAuthenticationResponse(
50-
L"userName", L"password" , &webAuthenticationResponse);
51-
args->put_Response(webAuthenticationResponse.get());
52-
49+
wil::com_ptr<ICoreWebView2Deferral> deferral;
50+
wil::com_ptr<ICoreWebView2WebAuthenticationRequestedEventArgs> web_auth_args = args;
51+
args->GetDeferral(&deferral);
52+
ShowCustomLoginUI().then([web_auth_args, deferral](LPCWSTR userName, LPCWSTR password) {
53+
wil::com_ptr<ICoreWebView2WebAuthenticationResponse> webAuthenticationResponse;
54+
webviewEnvironment->CreateWebAuthenticationResponse(
55+
userName, password, &webAuthenticationResponse);
56+
args->put_Response(webAuthenticationResponse.get());
57+
deferral->Complete();
58+
}
59+
5360
return S_OK;
5461
})
5562
.Get(),
@@ -59,9 +66,12 @@ The developer can provide the authentication credentials on behalf of the user w
5966
```c#
6067
webView.CoreWebView2.WebAuthenticationRequested += delegate (object sender, CoreWebView2WebAuthenticationRequestedEventArgs args)
6168
{
69+
CoreWebView2Deferral deferral = args.GetDeferral();
70+
Credential credential = await ShowCustomLoginUIAsync();
6271
CoreWebView2WebAuthenticationResponse response = _coreWebView2Environment.CreateWebAuthenticationResponse(
63-
"User", "Pass");
72+
credential.username, credential.password);
6473
args.Response = response;
74+
deferral.Complete();
6575
};
6676
```
6777

@@ -101,8 +111,7 @@ webview2->add_WebAuthenticationRequested(
101111
ICoreWebView2WebAuthenticationRequestedEventArgs* args)
102112
{
103113
args->get_Challenge(&challenge);
104-
if (wcsncmp(challenge.get(), L"Expected login credentials") != 0)
105-
{
114+
if (!ValidateChallenge(challenge.get())) { // Check the challenge string
106115
args->put_Cancel(true);
107116
}
108117
return S_OK;
@@ -203,6 +212,10 @@ interface ICoreWebView2WebAuthenticationRequestedEventArgs : IUnknown
203212
[propget] HRESULT Cancel([out, retval] BOOL* cancel);
204213
/// Set the Cancel property.
205214
[propput] HRESULT Cancel([in] BOOL cancel);
215+
216+
/// Returns an `ICoreWebView2Deferral` object. Use this operation to
217+
/// complete the event at a later time.
218+
HRESULT GetDeferral([out, retval] ICoreWebView2Deferral** deferral);
206219
}
207220
208221
[uuid(0cec3e32-36aa-4859-9bbe-f9c116ad4721), object, pointer_default(unique)]

0 commit comments

Comments
 (0)