Last active
August 15, 2017 11:45
-
-
Save shammelburg/7b0f956cc48514ce8bd14045445337e0 to your computer and use it in GitHub Desktop.
Windows Auth CORS for .Net Core
This file contains hidden or 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
// [DisableCors] | |
[Authorize] | |
[Route("api/[controller]")] | |
public class AuthController : Controller | |
{ | |
private AuthicationSettings _authSettings { get; set; } | |
public AuthController(IOptions<AuthicationSettings> settings) | |
{ | |
_authSettings = settings.Value; | |
} | |
// GET: api/values | |
[HttpGet] | |
public IActionResult Get() | |
{ | |
var isAdmin = User.IsInRole(_authSettings.Global); | |
return Ok(new { User = User.Identity.Name, IsAdmin = isAdmin }); | |
} | |
} |
This file contains hidden or 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
// Add framework services. | |
services.AddCors(options => | |
{ | |
options.AddPolicy("AllowSpecificOrigin", | |
builder => builder | |
.WithOrigins(Configuration["CORS:ClientEndpoint"]) | |
.AllowAnyMethod() | |
.AllowAnyHeader() | |
.AllowCredentials()); | |
}); | |
// CORS | |
services.Configure<MvcOptions>(options => { options.Filters.Add(new CorsAuthorizationFilterFactory("AllowSpecificOrigin")); }); | |
// Windows Authentication | |
services.AddAuthentication("Windows"); | |
//services.Configure<IISOptions>(options => options.ForwardWindowsAuthentication = true); | |
//services.AddAuthorization(options => { options.AddPolicy("AllUsers", policy => policy.RequireAuthenticatedUser()); }); | |
app.UseAuthentication(); |
This file contains hidden or 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
// for server testing/live | |
IIS Site > Authentication > { Anonymous = true, windows = true } |
This file contains hidden or 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
// for local dev | |
{ | |
"iisSettings": { | |
"windowsAuthentication": true, | |
"anonymousAuthentication": true, | |
"iisExpress": { | |
"applicationUrl": "http://localhost:port/", | |
"sslPort": 0 | |
} | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<system.webServer> | |
<handlers> | |
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> | |
</handlers> | |
<aspNetCore forwardWindowsAuthToken="true" processPath="dotnet" arguments=".\BrandedSitesCmsCoreApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" /> | |
</system.webServer> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment