From 6e7ec9b2fb6e7c1c991056f8ed6eb77627c3fa0a Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Fri, 1 May 2015 17:00:06 -0700 Subject: [PATCH] Cleanup cookies (moar var) --- .../CookieAuthenticationHandler.cs | 49 +++++++++---------- .../CookieApplyRedirectContext.cs | 1 - .../CookieAuthenticationNotifications.cs | 3 +- .../CookieResponseSignInContext.cs | 1 - .../CookieResponseSignOutContext.cs | 1 - .../Notifications/DefaultBehavior.cs | 6 +-- 6 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationHandler.cs b/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationHandler.cs index 2e71d1c0e6..cc1dc91685 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationHandler.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/CookieAuthenticationHandler.cs @@ -8,7 +8,6 @@ using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; -using Microsoft.Framework.Internal; using Microsoft.Framework.Logging; namespace Microsoft.AspNet.Authentication.Cookies @@ -37,7 +36,7 @@ namespace Microsoft.AspNet.Authentication.Cookies AuthenticationTicket ticket = null; try { - string cookie = Options.CookieManager.GetRequestCookie(Context, Options.CookieName); + var cookie = Options.CookieManager.GetRequestCookie(Context, Options.CookieName); if (string.IsNullOrWhiteSpace(cookie)) { return null; @@ -53,7 +52,7 @@ namespace Microsoft.AspNet.Authentication.Cookies if (Options.SessionStore != null) { - Claim claim = ticket.Principal.Claims.FirstOrDefault(c => c.Type.Equals(SessionIdClaim)); + var claim = ticket.Principal.Claims.FirstOrDefault(c => c.Type.Equals(SessionIdClaim)); if (claim == null) { Logger.LogWarning(@"SessionId missing"); @@ -68,9 +67,9 @@ namespace Microsoft.AspNet.Authentication.Cookies } } - DateTimeOffset currentUtc = Options.SystemClock.UtcNow; - DateTimeOffset? issuedUtc = ticket.Properties.IssuedUtc; - DateTimeOffset? expiresUtc = ticket.Properties.ExpiresUtc; + var currentUtc = Options.SystemClock.UtcNow; + var issuedUtc = ticket.Properties.IssuedUtc; + var expiresUtc = ticket.Properties.ExpiresUtc; if (expiresUtc != null && expiresUtc.Value < currentUtc) { @@ -81,17 +80,17 @@ namespace Microsoft.AspNet.Authentication.Cookies return null; } - bool allowRefresh = ticket.Properties.AllowRefresh ?? true; + var allowRefresh = ticket.Properties.AllowRefresh ?? true; if (issuedUtc != null && expiresUtc != null && Options.SlidingExpiration && allowRefresh) { - TimeSpan timeElapsed = currentUtc.Subtract(issuedUtc.Value); - TimeSpan timeRemaining = expiresUtc.Value.Subtract(currentUtc); + var timeElapsed = currentUtc.Subtract(issuedUtc.Value); + var timeRemaining = expiresUtc.Value.Subtract(currentUtc); if (timeRemaining < timeElapsed) { _shouldRenew = true; _renewIssuedUtc = currentUtc; - TimeSpan timeSpan = expiresUtc.Value.Subtract(issuedUtc.Value); + var timeSpan = expiresUtc.Value.Subtract(issuedUtc.Value); _renewExpiresUtc = currentUtc.Add(timeSpan); } } @@ -104,7 +103,7 @@ namespace Microsoft.AspNet.Authentication.Cookies } catch (Exception exception) { - CookieExceptionContext exceptionContext = new CookieExceptionContext(Context, Options, + var exceptionContext = new CookieExceptionContext(Context, Options, CookieExceptionContext.ExceptionLocation.Authenticate, exception, ticket); Options.Notifications.Exception(exceptionContext); if (exceptionContext.Rethrow) @@ -123,16 +122,16 @@ namespace Microsoft.AspNet.Authentication.Cookies protected override async Task ApplyResponseGrantAsync() { var signin = SignInContext; - bool shouldSignin = signin != null; + var shouldSignin = signin != null; var signout = SignOutContext; - bool shouldSignout = signout != null; + var shouldSignout = signout != null; if (!(shouldSignin || shouldSignout || _shouldRenew)) { return; } - AuthenticationTicket model = await AuthenticateAsync(); + var model = await AuthenticateAsync(); try { var cookieOptions = new CookieOptions @@ -180,7 +179,7 @@ namespace Microsoft.AspNet.Authentication.Cookies if (signInContext.Properties.IsPersistent) { - DateTimeOffset expiresUtc = signInContext.Properties.ExpiresUtc ?? issuedUtc.Add(Options.ExpireTimeSpan); + var expiresUtc = signInContext.Properties.ExpiresUtc ?? issuedUtc.Add(Options.ExpireTimeSpan); signInContext.CookieOptions.Expires = expiresUtc.ToUniversalTime().DateTime; } @@ -198,7 +197,7 @@ namespace Microsoft.AspNet.Authentication.Cookies Options.AuthenticationScheme)); model = new AuthenticationTicket(principal, null, Options.AuthenticationScheme); } - string cookieValue = Options.TicketDataFormat.Protect(model); + var cookieValue = Options.TicketDataFormat.Protect(model); Options.CookieManager.AppendResponseCookie( Context, @@ -249,7 +248,7 @@ namespace Microsoft.AspNet.Authentication.Cookies model = new AuthenticationTicket(principal, null, Options.AuthenticationScheme); } - string cookieValue = Options.TicketDataFormat.Protect(model); + var cookieValue = Options.TicketDataFormat.Protect(model); if (model.Properties.IsPersistent) { @@ -275,13 +274,13 @@ namespace Microsoft.AspNet.Authentication.Cookies HeaderNameExpires, HeaderValueMinusOne); - bool shouldLoginRedirect = shouldSignin && Options.LoginPath.HasValue && Request.Path == Options.LoginPath; - bool shouldLogoutRedirect = shouldSignout && Options.LogoutPath.HasValue && Request.Path == Options.LogoutPath; + var shouldLoginRedirect = shouldSignin && Options.LoginPath.HasValue && Request.Path == Options.LoginPath; + var shouldLogoutRedirect = shouldSignout && Options.LogoutPath.HasValue && Request.Path == Options.LogoutPath; if ((shouldLoginRedirect || shouldLogoutRedirect) && Response.StatusCode == 200) { - IReadableStringCollection query = Request.Query; - string redirectUri = query.Get(Options.ReturnUrlParameter); + var query = Request.Query; + var redirectUri = query.Get(Options.ReturnUrlParameter); if (!string.IsNullOrWhiteSpace(redirectUri) && IsHostRelative(redirectUri)) { @@ -292,7 +291,7 @@ namespace Microsoft.AspNet.Authentication.Cookies } catch (Exception exception) { - CookieExceptionContext exceptionContext = new CookieExceptionContext(Context, Options, + var exceptionContext = new CookieExceptionContext(Context, Options, CookieExceptionContext.ExceptionLocation.ApplyResponseGrant, exception, model); Options.Notifications.Exception(exceptionContext); if (exceptionContext.Rethrow) @@ -363,7 +362,7 @@ namespace Microsoft.AspNet.Authentication.Cookies return; } - string loginUri = string.Empty; + var loginUri = string.Empty; if (ChallengeContext != null) { loginUri = new AuthenticationProperties(ChallengeContext.Properties).RedirectUri; @@ -373,7 +372,7 @@ namespace Microsoft.AspNet.Authentication.Cookies { if (string.IsNullOrWhiteSpace(loginUri)) { - string currentUri = + var currentUri = Request.PathBase + Request.Path + Request.QueryString; @@ -392,7 +391,7 @@ namespace Microsoft.AspNet.Authentication.Cookies } catch (Exception exception) { - CookieExceptionContext exceptionContext = new CookieExceptionContext(Context, Options, + var exceptionContext = new CookieExceptionContext(Context, Options, CookieExceptionContext.ExceptionLocation.ApplyResponseChallenge, exception, ticket: null); Options.Notifications.Exception(exceptionContext); if (exceptionContext.Rethrow) diff --git a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieApplyRedirectContext.cs b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieApplyRedirectContext.cs index cc6364817a..9083ed817c 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieApplyRedirectContext.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieApplyRedirectContext.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - using System.Diagnostics.CodeAnalysis; using Microsoft.AspNet.Http; using Microsoft.AspNet.Authentication.Notifications; diff --git a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieAuthenticationNotifications.cs b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieAuthenticationNotifications.cs index dc287484a4..0c875b2083 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieAuthenticationNotifications.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieAuthenticationNotifications.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - using System; using System.Threading.Tasks; @@ -112,4 +111,4 @@ namespace Microsoft.AspNet.Authentication.Cookies OnException.Invoke(context); } } -} +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieResponseSignInContext.cs b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieResponseSignInContext.cs index e9d7b10590..f894231ae8 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieResponseSignInContext.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieResponseSignInContext.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - using System.Security.Claims; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; diff --git a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieResponseSignOutContext.cs b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieResponseSignOutContext.cs index 28dce3cdf3..260a31ad9f 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieResponseSignOutContext.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/CookieResponseSignOutContext.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - using Microsoft.AspNet.Http; using Microsoft.AspNet.Authentication.Notifications; diff --git a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/DefaultBehavior.cs b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/DefaultBehavior.cs index 77d537710b..9807bbdf4f 100644 --- a/src/Microsoft.AspNet.Authentication.Cookies/Notifications/DefaultBehavior.cs +++ b/src/Microsoft.AspNet.Authentication.Cookies/Notifications/DefaultBehavior.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNet.Authentication.Cookies { if (IsAjaxRequest(context.Request)) { - string jsonResponse = JsonConvert.SerializeObject(new + var jsonResponse = JsonConvert.SerializeObject(new { status = context.Response.StatusCode, headers = new @@ -34,7 +34,7 @@ namespace Microsoft.AspNet.Authentication.Cookies private static bool IsAjaxRequest(HttpRequest request) { - IReadableStringCollection query = request.Query; + var query = request.Query; if (query != null) { if (query["X-Requested-With"] == "XMLHttpRequest") @@ -43,7 +43,7 @@ namespace Microsoft.AspNet.Authentication.Cookies } } - IHeaderDictionary headers = request.Headers; + var headers = request.Headers; if (headers != null) { if (headers["X-Requested-With"] == "XMLHttpRequest")