Skip to content

Instantly share code, notes, and snippets.

@shammelburg
Last active August 15, 2017 11:45
Show Gist options
  • Save shammelburg/7b0f956cc48514ce8bd14045445337e0 to your computer and use it in GitHub Desktop.
Save shammelburg/7b0f956cc48514ce8bd14045445337e0 to your computer and use it in GitHub Desktop.
Windows Auth CORS for .Net Core
// [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 });
}
}
// 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();
// for server testing/live
IIS Site > Authentication > { Anonymous = true, windows = true }
// for local dev
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:port/",
"sslPort": 0
}
}
}
<?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