This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CORS support out of MVC Core
- Loading branch information
Showing
8 changed files
with
475 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Microsoft.AspNetCore.Mvc.Cors/Internal/CorsHttpMethodActionConstraint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using Microsoft.AspNetCore.Mvc.ActionConstraints; | ||
using Microsoft.Extensions.Primitives; | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Cors.Internal | ||
{ | ||
public class CorsHttpMethodActionConstraint : HttpMethodActionConstraint | ||
{ | ||
private readonly string OriginHeader = "Origin"; | ||
private readonly string AccessControlRequestMethod = "Access-Control-Request-Method"; | ||
private readonly string PreflightHttpMethod = "OPTIONS"; | ||
|
||
public CorsHttpMethodActionConstraint(HttpMethodActionConstraint constraint) | ||
: base(constraint.HttpMethods) | ||
{ | ||
} | ||
|
||
public override bool Accept(ActionConstraintContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
var methods = (ReadOnlyCollection<string>)HttpMethods; | ||
if (methods.Count == 0) | ||
{ | ||
return true; | ||
} | ||
|
||
var request = context.RouteContext.HttpContext.Request; | ||
if (request.Headers.ContainsKey(OriginHeader) && | ||
string.Equals(request.Method, PreflightHttpMethod, StringComparison.OrdinalIgnoreCase) && | ||
request.Headers.TryGetValue(AccessControlRequestMethod, out var accessControlRequestMethod) && | ||
!StringValues.IsNullOrEmpty(accessControlRequestMethod)) | ||
{ | ||
for (var i = 0; i < methods.Count; i++) | ||
{ | ||
var supportedMethod = methods[i]; | ||
if (string.Equals(supportedMethod, accessControlRequestMethod, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
return base.Accept(context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.