Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit e5518e6

Browse files
committed
ChallengeContext will be null with [Authorize] attribute
OpenIdConnect set Ticket.Principal, get identity from there.
1 parent d7b389e commit e5518e6

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/Microsoft.AspNet.Security.OpenIdConnect/OpenidConnectAuthenticationHandler.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ protected override async Task ApplyResponseGrantAsync()
9191
{
9292
ProtocolMessage = openIdConnectMessage
9393
};
94+
9495
await Options.Notifications.RedirectToIdentityProvider(notification);
9596

9697
if (!notification.HandledResponse)
@@ -100,6 +101,7 @@ protected override async Task ApplyResponseGrantAsync()
100101
{
101102
_logger.WriteWarning("The logout redirect URI is malformed: " + redirectUri);
102103
}
104+
103105
Response.Redirect(redirectUri);
104106
}
105107
}
@@ -116,15 +118,30 @@ protected override void ApplyResponseChallenge()
116118
/// <returns></returns>
117119
protected override async Task ApplyResponseChallengeAsync()
118120
{
119-
if ((Response.StatusCode != 401) || (ChallengeContext == null))
121+
if (Response.StatusCode != 401)
122+
{
123+
return;
124+
}
125+
126+
// Active middleware should redirect on 401 even if there wasn't an explicit challenge.
127+
if (ChallengeContext == null && Options.AuthenticationMode == AuthenticationMode.Passive)
120128
{
121129
return;
122130
}
123131

124132
// order for redirect_uri
125133
// 1. challenge.Properties.RedirectUri
126134
// 2. CurrentUri
127-
AuthenticationProperties properties = new AuthenticationProperties(ChallengeContext.Properties);
135+
AuthenticationProperties properties;
136+
if (ChallengeContext == null)
137+
{
138+
properties = new AuthenticationProperties();
139+
}
140+
else
141+
{
142+
properties = new AuthenticationProperties(ChallengeContext.Properties);
143+
}
144+
128145
if (string.IsNullOrEmpty(properties.RedirectUri))
129146
{
130147
properties.RedirectUri = CurrentUri;
@@ -154,7 +171,6 @@ protected override async Task ApplyResponseChallengeAsync()
154171
State = OpenIdConnectAuthenticationDefaults.AuthenticationPropertiesKey + "=" + Uri.EscapeDataString(Options.StateDataFormat.Protect(properties))
155172
};
156173

157-
// TODO - brentschmaltz, if INonceCache is set should we even consider if ProtocolValidator is set?
158174
if (Options.ProtocolValidator.RequireNonce)
159175
{
160176
openIdConnectMessage.Nonce = Options.ProtocolValidator.GenerateNonce();
@@ -179,7 +195,7 @@ protected override async Task ApplyResponseChallengeAsync()
179195
string redirectUri = notification.ProtocolMessage.CreateAuthenticationRequestUrl();
180196
if (!Uri.IsWellFormedUriString(redirectUri, UriKind.Absolute))
181197
{
182-
_logger.WriteWarning("The authenticate redirect URI is malformed: " + redirectUri);
198+
_logger.WriteWarning("Uri.IsWellFormedUriString(redirectUri, UriKind.Absolute) returned 'false', redirectUri is: " + (redirectUri ?? "null"));
183199
}
184200

185201
Response.Redirect(redirectUri);

src/Microsoft.AspNet.Security/Infrastructure/AuthenticationHandler.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ protected async Task BaseInitializeAsync(AuthenticationOptions options, HttpCont
7777
if (BaseOptions.AuthenticationMode == AuthenticationMode.Active)
7878
{
7979
AuthenticationTicket ticket = await AuthenticateAsync();
80-
if (ticket != null && ticket.Identity != null)
80+
if (ticket != null)
8181
{
82-
SecurityHelper.AddUserIdentity(Context, ticket.Identity);
82+
if ( ticket.Identity != null)
83+
SecurityHelper.AddUserIdentity(Context, ticket.Identity);
84+
else if (ticket.Principal != null)
85+
SecurityHelper.AddUserIdentity(Context, ticket.Principal.Identity);
8386
}
8487
}
8588
}

0 commit comments

Comments
 (0)