No answers, so i'll sketch in my own results of reading around - I have yet to test properly, but the schema I am thinking of using is
in Startup.Auth replace app.UseCookieAuthentication with
app.UseCookieAuthentication(new CookieAuthenticationOptions { Provider = new AngularCoookieAuthProvider() });
AngularCookieAuthProvider.cs
using Microsoft.Owin.Security.Cookies;
using System.Web.Helpers;
public class AngularCookieAuthProvider: CookieAuthenticationProvider
{
public const string AngularHeaderTokenName = "XSRF-TOKEN";
public const string AngularCookieTokenName = "X-XSRF-TOKEN";
public override void ResponseSignedIn(CookieResponseSignedInContext context)
{
SetAntiCsfrTokens(context.Response);
base.ResponseSignedIn(context);
}
public override void ResponseSignOut(CookieResponseSignOutContext context)
{
context.Response.Cookies.Delete(AngularCookieTokenName);
context.Response.Headers.Remove(AngularHeaderTokenName);
base.ResponseSignOut(context);
}
internal static void SetAntiCsfrTokens(IOwinResponse response, string oldCookieToken=null)
{
string cookieToken;
string formToken;
AntiForgery.GetTokens(oldCookieToken, out cookieToken, out formToken);
response.Cookies.Append(AngularCookieTokenName, cookieToken);
response.Headers.Append(AngularHeaderTokenName, formToken);
}
}
and our CheckCsrfHeaderAttribute.cs:
using System.Linq;
using System.Net.Http;
using System.Web.Helpers;
using System.Web.Http;
using System.Web.Http.Controllers;
public class CheckCsrfHeaderAttribute : AuthorizeAttribute
{
// http://stackoverflow.com/questions/11725988/problems-implementing-validatingantiforgerytoken-attribute-for-web-api-with-mvc
protected override bool IsAuthorized(HttpActionContext context)
{
var owinContext = context.Request.GetOwinContext();
var request = owinContext.Request;
// get auth token from cookie
var authCookie = request.Cookies[AngularCoookieAuthProvider.AngularCookieTokenName];
var csrfToken = request.Headers.GetValues(AngularCoookieAuthProvider.AngularHeaderTokenName).FirstOrDefault();
// Verify that csrf token was generated from auth token
// Since the csrf token should have gone out as a cookie, only our site should have been able to get it (via javascript) and return it in a header.
// This proves that our site made the request.
AntiForgery.Validate(csrfToken, authCookie);//should throw if a problem
AngularCoookieAuthProvider.SetAntiCsfrTokens(owinContext.Response, authCookie);
return true;
}
}