Skip to content

Commit c11628f

Browse files
author
Cagri Yildirim
committed
Update WebAuthenticationRequested.md
Rev 2
1 parent 3f6b525 commit c11628f

1 file changed

Lines changed: 58 additions & 80 deletions

File tree

specs/WebAuthenticationRequested.md

Lines changed: 58 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -16,70 +16,37 @@
1616
docs.microsoft.com (https://docs.microsoft.com/en-us/microsoft-edge/webview2/).
1717
Hopefully we'll be able to copy it mostly verbatim.
1818
So the second audience is everyone that reads there to learn how
19-
and why to use this API.
19+
and why to use this API.
2020
-->
2121

2222

2323
# Background
24-
By default HTTP basic authentication requests inside WebView2 show the authentication UI, which is a dialog prompt in which the user can type in user name and password credentials just like in the Edge browser. We have been requested by WebView2 app developers to provide finer granularity for managing HTTP Basic authentications inside WebView2, including the ability to hide the login UI and provide credentials.
24+
By default HTTP basic authentication requests inside WebView2 show the authentication UI, which is a dialog prompt in which the user can type in user name and password credentials just like in the Edge browser. We have been requested by WebView2 app developers to provide finer granularity for managing HTTP Basic authentications inside WebView2, including the ability to hide the login UI and provide credentials.
2525

2626

2727
# Description
2828
We propose a new event for WebView2, CoreWebView2WebAuthenticationRequested that will allow developers to listen on and override the HTTP Basic authentication requests in WebView2. When there is a HTTP Basic authentication request in WebView2, the developer will have a choice to:
2929
1) Provide credentials
3030
2) Cancel the login altogether
31-
3) Ask the user for credentials via the default login prompt
32-
We also propose CoreWebView2StagingWebAuthenticationResponse, the runtime class that represents the app's response with credentials to the basic authentication request.
31+
3) Ask the user for credentials via the default login prompt
32+
We also propose CoreWebView2WebAuthenticationResponse, the runtime class that represents the app's response with credentials to the basic authentication request.
3333

3434
# Examples
35-
## Basic usage
36-
CoreWebView2WebAuthenticationRequestedEvent follows the same event handling pattern as any other WebView2 event.
37-
38-
To add event handler:
39-
```cpp
40-
webviewStaging2->add_WebAuthenticationRequested(
41-
Callback<ICoreWebView2StagingWebAuthenticationRequestedEventHandler>(
42-
[this](
43-
ICoreWebView2Staging2* sender,
44-
ICoreWebView2StagingWebAuthenticationRequestedEventArgs* args) {
45-
// Handler code
46-
return S_OK;
47-
})
48-
.Get(),
49-
&m_webAuthenticationRequestedToken));
50-
```
51-
52-
```c#
53-
public OnWebAuthenticationRequested(object sender, CoreWebView2WebAuthenticationRequestedEventArgs args)
54-
{
55-
// Handler code
56-
};
57-
webView.CoreWebView2.WebAuthenticationRequested += OnWebAuthenticationRequested;
58-
```
59-
60-
To remove event handler:
61-
```cpp
62-
webViewStaging2->remove_WebAuthenticationRequested(m_webAuthenticationRequestedToken);
63-
```
64-
65-
```c#
66-
webView.CoreWebView2.WebAuthenticationRequested -= OnWebAuthenticationRequested;
67-
```
68-
6935
## Provide credentials
70-
The developer can provide the authentication credentials on behalf of the user when it encounters the Basic authentication request. In this case, the 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.
36+
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.
7137

7238
```cpp
73-
webviewStaging2->add_WebAuthenticationRequested(
74-
Callback<ICoreWebView2StagingWebAuthenticationRequestedEventHandler>(
39+
webview2->add_WebAuthenticationRequested(
40+
Callback<ICoreWebView2WebAuthenticationRequestedEventHandler>(
7541
[this](
76-
ICoreWebView2Staging2* sender,
77-
ICoreWebView2StagingWebAuthenticationRequestedEventArgs* args) {
78-
wil::com_ptr<ICoreWebView2StagingEnvironment> webviewEnvironmentStaging;
42+
ICoreWebView2* sender,
43+
ICoreWebView2WebAuthenticationRequestedEventArgs* args)
44+
{
45+
wil::com_ptr<ICoreWebView2Environment> webviewEnvironment;
7946
m_appWindow->GetWebViewEnvironment()->QueryInterface(
80-
IID_PPV_ARGS(&webviewEnvironmentStaging));
81-
wil::com_ptr<ICoreWebView2StagingWebAuthenticationResponse> webAuthenticationResponse;
82-
webviewEnvironmentStaging->CreateWebAuthenticationResponse(
47+
IID_PPV_ARGS(&webviewEnvironment));
48+
wil::com_ptr<ICoreWebView2WebAuthenticationResponse> webAuthenticationResponse;
49+
webviewEnvironment->CreateWebAuthenticationResponse(
8350
L"userName", L"password" , &webAuthenticationResponse);
8451
args->put_Response(webAuthenticationResponse.get());
8552

@@ -99,14 +66,15 @@ webView.CoreWebView2.WebAuthenticationRequested += delegate (object sender, Core
9966
```
10067

10168
## Cancel authentication prompt
102-
The developer can block the authentication request. In this case, the login dialog prompt will no longer be shown to the user and the server will respond as if the user clicked cancel.
69+
The developer can block the authentication request. In this case, the default login dialog prompt will no longer be shown to the user and the server will respond as if the user clicked cancel.
10370

10471
```cpp
105-
webviewStaging2->add_WebAuthenticationRequested(
106-
Callback<ICoreWebView2StagingWebAuthenticationRequestedEventHandler>(
72+
webview2->add_WebAuthenticationRequested(
73+
Callback<ICoreWebView2WebAuthenticationRequestedEventHandler>(
10774
[this](
108-
ICoreWebView2Staging2* sender,
109-
ICoreWebView2StagingWebAuthenticationRequestedEventArgs* args) {
75+
ICoreWebView2* sender,
76+
ICoreWebView2WebAuthenticationRequestedEventArgs* args)
77+
{
11078
args->put_Cancel(true);
11179

11280
return S_OK;
@@ -123,18 +91,20 @@ webView.CoreWebView2.WebAuthenticationRequested += delegate (object sender, Core
12391
```
12492

12593
## Read authorization challenge string
126-
Developer can read the authorization challenge string sent by server. Note that if the developer doesn't cancel or provide a response, the login dialog prompt will be shown to the user.
94+
Developer can read the authorization challenge string sent by server. Note that if the developer doesn't cancel or provide a response, the default login dialog prompt will be shown to the user.
12795

12896
```cpp
129-
webviewStaging2->add_WebAuthenticationRequested(
130-
Callback<ICoreWebView2StagingWebAuthenticationRequestedEventHandler>(
97+
webview2->add_WebAuthenticationRequested(
98+
Callback<ICoreWebView2WebAuthenticationRequestedEventHandler>(
13199
[this](
132-
ICoreWebView2Staging2* sender,
133-
ICoreWebView2StagingWebAuthenticationRequestedEventArgs* args) {
100+
ICoreWebView2* sender,
101+
ICoreWebView2WebAuthenticationRequestedEventArgs* args)
102+
{
134103
args->get_Challenge(&challenge);
135-
if (wcsncmp(challenge.get(), L"Expected login credentials") != 0) {
104+
if (wcsncmp(challenge.get(), L"Expected login credentials") != 0)
105+
{
136106
args->put_Cancel(true);
137-
}
107+
}
138108
return S_OK;
139109
})
140110
.Get(),
@@ -146,7 +116,7 @@ webView.CoreWebView2.WebAuthenticationRequested += delegate (object sender, Core
146116
{
147117
if (args.Challenge.Equals("Expected login credentials")) {
148118
args.Cancel = true;
149-
}
119+
}
150120
};
151121
```
152122

@@ -158,40 +128,47 @@ webView.CoreWebView2.WebAuthenticationRequested += delegate (object sender, Core
158128

159129
# API Details
160130
```idl
161-
/// This is the ICoreWebView2 Staging interface.
162-
[uuid(9EAFB7D0-88C3-4450-BBFB-C05A46C40C72), object, pointer_default(unique)]
163-
interface ICoreWebView2Staging2 : IUnknown {
131+
/// WebView2 enables you to host web content using the latest Microsoft Edge
132+
/// browser and web technology.
133+
134+
[uuid(76eceacb-0462-4d94-ac83-423a6793775e), object, pointer_default(unique)]
135+
interface ICoreWebView2 : IUnknown
136+
{
137+
/// ...
138+
164139
/// Add an event handler for the WebAuthenticationRequested event.
165-
/// WebAuthenticationRequested event fires when WebView encountered a Basic HTTP
166-
/// Authentication request.
140+
/// WebAuthenticationRequested event is raised when WebView encountered a Basic HTTP
141+
/// Authentication request as described in
142+
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication.
167143
///
168144
/// The host can provide a response with credentials for the authentication or
169-
/// cancel the request. If the host doesn't handle the event, WebView will show
170-
/// the authorization challenge dialog prompt to user.
145+
/// cancel the request. If the host doesn't set the Cancel property to true or
146+
/// set the Response property, then WebView2 will show the default
147+
/// authorization challenge dialog prompt to the user.
171148
///
172149
HRESULT add_WebAuthenticationRequested(
173-
[in] ICoreWebView2StagingWebAuthenticationRequestedEventHandler* eventHandler,
150+
[in] ICoreWebView2WebAuthenticationRequestedEventHandler* eventHandler,
174151
[out] EventRegistrationToken* token);
175152
/// Remove an event handler previously added with add_WebResourceRequested.
176153
HRESULT remove_WebAuthenticationRequested(
177154
[in] EventRegistrationToken token);
178155
}
179156
180-
/// This is the CoreWebView2WebAuthenticationRequestedEventHandler Staging interface
157+
/// This is the CoreWebView2WebAuthenticationRequestedEventHandler interface
181158
[uuid(f87e5d35-3248-406b-81dd-1c36aab8081d), object, pointer_default(unique)]
182-
interface ICoreWebView2StagingWebAuthenticationRequestedEventHandler : IUnknown
159+
interface ICoreWebView2WebAuthenticationRequestedEventHandler : IUnknown
183160
{
184161
/// Called to provide the implementer with the event args for the
185162
/// corresponding event.
186163
HRESULT Invoke(
187-
[in] ICoreWebView2Staging2* sender,
188-
[in] ICoreWebView2StagingWebAuthenticationRequestedEventArgs* args);
164+
[in] ICoreWebView2* sender,
165+
[in] ICoreWebView2WebAuthenticationRequestedEventArgs* args);
189166
}
190167
191168
/// Represents a Basic HTTP authentication response that contains a user name
192169
/// and a password as according to RFC7617 (https://tools.ietf.org/html/rfc7617)
193170
[uuid(bc9cfd60-29c4-4943-a83b-d0d2f3e7df03), object, pointer_default(unique)]
194-
interface ICoreWebView2StagingWebAuthenticationResponse : IUnknown
171+
interface ICoreWebView2WebAuthenticationResponse : IUnknown
195172
{
196173
/// User name provided for authorization.
197174
[propget] HRESULT UserName([out, retval] LPWSTR* userName);
@@ -208,7 +185,7 @@ interface ICoreWebView2StagingWebAuthenticationResponse : IUnknown
208185
/// request that led to the HTTP authorization challenge, the challenge
209186
/// and allows the host to provide authentication response or cancel the request.
210187
[uuid(51d3adaa-159f-4e48-ad39-a86beb2c1435), object, pointer_default(unique)]
211-
interface ICoreWebView2StagingWebAuthenticationRequestedEventArgs : IUnknown
188+
interface ICoreWebView2WebAuthenticationRequestedEventArgs : IUnknown
212189
{
213190
/// The web resource request that led to the authorization challenge
214191
[propget] HRESULT Request([out, retval] ICoreWebView2WebResourceRequest** request);
@@ -217,9 +194,9 @@ interface ICoreWebView2StagingWebAuthenticationRequestedEventArgs : IUnknown
217194
[propget] HRESULT Challenge([out, retval] LPWSTR* challenge);
218195
219196
/// Response to the authentication request with credentials.
220-
[propget] HRESULT Response([out, retval] ICoreWebView2StagingWebAuthenticationResponse** response);
197+
[propget] HRESULT Response([out, retval] ICoreWebView2WebAuthenticationResponse** response);
221198
/// Set the Response property.
222-
[propput] HRESULT Response([in] ICoreWebView2StagingWebAuthenticationResponse* response);
199+
[propput] HRESULT Response([in] ICoreWebView2WebAuthenticationResponse* response);
223200
224201
/// Cancel the authentication request. False by default.
225202
/// If set to true, Response will be ignored.
@@ -229,13 +206,14 @@ interface ICoreWebView2StagingWebAuthenticationRequestedEventArgs : IUnknown
229206
}
230207
231208
[uuid(0cec3e32-36aa-4859-9bbe-f9c116ad4721), object, pointer_default(unique)]
232-
interface ICoreWebView2StagingEnvironment : IUnknown {
209+
interface ICoreWebView2Environment : IUnknown
210+
{
233211
/// Create a WebAuthenticationResponse object used to provide credentials for
234212
/// WebAuthenticationRequested event
235213
HRESULT CreateWebAuthenticationResponse(
236214
[in] LPCWSTR userName,
237215
[in] LPCWSTR password,
238-
[out, retval] ICoreWebView2StagingWebAuthenticationResponse** response);
216+
[out, retval] ICoreWebView2WebAuthenticationResponse** response);
239217
}
240218
241219
```
@@ -264,7 +242,7 @@ namespace Microsoft.Web.WebView2.Core
264242

265243
/// The HTTP basic authorization challenge string
266244
String Challenge { get; };
267-
245+
268246
/// Cancel the authentication request. False by default.
269247
/// If set to true, Response will be ignored.
270248
bool Cancel { get; set; };
@@ -289,7 +267,7 @@ namespace Microsoft.Web.WebView2.Core
289267
...
290268

291269
/// Add an event handler for the WebAuthenticationRequested event.
292-
/// WebAuthenticationRequested event fires when WebView encountered a Basic HTTP
270+
/// WebAuthenticationRequested event is raised when WebView encountered a Basic HTTP
293271
/// Authentication request.
294272
///
295273
/// The host can provide a response with credentials for the authentication or

0 commit comments

Comments
 (0)