From b7c8af8503831ba4becaa93b9453510b81c4d30e Mon Sep 17 00:00:00 2001 From: Praburaj Date: Fri, 6 Mar 2015 17:22:49 -0800 Subject: [PATCH] Reading AuthenticationProperties from SignOutContext This will enable users to set a specific redirect uri and call signout. --- .../OpenidConnectAuthenticationHandler.cs | 11 ++++----- .../OpenIdConnectMiddlewareTests.cs | 23 +++++++++++++++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenidConnectAuthenticationHandler.cs b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenidConnectAuthenticationHandler.cs index 046232b9ea..68e9bea24a 100644 --- a/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenidConnectAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Authentication.OpenIdConnect/OpenidConnectAuthenticationHandler.cs @@ -8,10 +8,9 @@ using System.IO; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; +using Microsoft.AspNet.Authentication.Notifications; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; -using Microsoft.AspNet.Authentication; -using Microsoft.AspNet.Authentication.Notifications; using Microsoft.Framework.Logging; using Microsoft.IdentityModel.Protocols; @@ -77,8 +76,8 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect // Set End_Session_Endpoint in order: // 1. properties.Redirect // 2. Options.Wreply - AuthenticationProperties properties = new AuthenticationProperties(); - if (properties != null && !string.IsNullOrEmpty(properties.RedirectUri)) + var properties = new AuthenticationProperties(signout.Properties); + if (!string.IsNullOrEmpty(properties.RedirectUri)) { openIdConnectMessage.PostLogoutRedirectUri = properties.RedirectUri; } @@ -220,7 +219,7 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect } OpenIdConnectMessage openIdConnectMessage = null; - + // assumption: if the ContentType is "application/x-www-form-urlencoded" it should be safe to read as it is small. if (string.Equals(Request.Method, "POST", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(Request.ContentType) @@ -580,4 +579,4 @@ namespace Microsoft.AspNet.Authentication.OpenIdConnect return false; } } -} +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs index 96f17c86ea..7681d54668 100644 --- a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs @@ -125,7 +125,6 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect [Fact] public async Task SignOutWithDefaultRedirectUri() { - ISecureDataFormat stateFormat = new PropertiesDataFormat(new EphemeralDataProtectionProvider().CreateProtector("GoogleTest")); var server = CreateServer(options => { options.Authority = "https://login.windows.net/common"; @@ -140,7 +139,6 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect [Fact] public async Task SignOutWithCustomRedirectUri() { - ISecureDataFormat stateFormat = new PropertiesDataFormat(new EphemeralDataProtectionProvider().CreateProtector("GoogleTest")); var server = CreateServer(options => { options.Authority = "https://login.windows.net/common"; @@ -153,6 +151,21 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect transaction.Response.Headers.Location.AbsoluteUri.ShouldContain(Uri.EscapeDataString("https://example.com/logout")); } + [Fact] + public async Task SignOutWith_Specific_RedirectUri_From_Authentication_Properites() + { + var server = CreateServer(options => + { + options.Authority = "https://login.windows.net/common"; + options.ClientId = "Test Id"; + options.PostLogoutRedirectUri = "https://example.com/logout"; + }); + + var transaction = await SendAsync(server, "https://example.com/signout_with_specific_redirect_uri"); + transaction.Response.StatusCode.ShouldBe(HttpStatusCode.Redirect); + transaction.Response.Headers.Location.AbsoluteUri.ShouldContain(Uri.EscapeDataString("http://www.example.com/specific_redirect_uri")); + } + [Fact] // Test Cases for calculating the expiration time of cookie from cookie name public void NonceCookieExpirationTime() @@ -212,6 +225,12 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect { res.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationScheme); } + else if (req.Path == new PathString("/signout_with_specific_redirect_uri")) + { + res.SignOut( + OpenIdConnectAuthenticationDefaults.AuthenticationScheme, + new AuthenticationProperties() { RedirectUri = "http://www.example.com/specific_redirect_uri" }); + } else if (handler != null) { await handler(context);