CORS für Web Api 2 und OWIN-Token-Authentifizierung aktivieren

Ich habe ein ASP.NET MVC 5-Webprojekt (localhost: 81), das Funktionen aus meinem WebApi 2-Projekt (localhost: 82) mithilfe von Knockoutjs aufruft, um die Kommunikation zwischen den beiden Projekten herzustellen, die ich für CORS aktiviere. Alles funktioniert soweit, bis ich versucht habe, die OWIN-Token-Authentifizierung für das WebApi zu implementieren.

Um den / token-Endpunkt auf dem WebApi zu verwenden, muss CORS auch auf dem Endpunkt aktiviert werden. Nach stundenlangem Ausprobieren und Suchen nach Lösungen funktioniert der Endpunkt jedoch immer noch und das api / token führt immer noch zu:

XMLHttpRequest cannot load http://localhost:82/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. 

public void Configuration(IAppBuilder app)
{
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    TokenConfig.ConfigureOAuth(app);
    ...
}

TokenConfig

public static void ConfigureOAuth(IAppBuilder app)
{
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);

    OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
        Provider = new SimpleAuthorizationServerProvider()
    };

    app.UseOAuthAuthorizationServer(OAuthServerOptions);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}

AuthorizationProvider

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

    var appUserManager = context.OwinContext.GetUserManager<AppUserManager>();
    IdentityUser user = await appUserManager.FindAsync(context.UserName, context.Password);

    if (user == null)
    {
        context.SetError("invalid_grant", "The user name or password is incorrect.");
        return;
    }
    ... claims
}

IdentityConfig

public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{
    // Tried to enable it again without success. 
    //context.Response.Headers.Add("Access-Control-Allow-Origin", new[] {"*"});

    var manager = new AppUserManager(new UserStore<AppUser>(context.Get<ApplicationDbContect>()));

    ...

    var dataProtectionProvider = options.DataProtectionProvider;
    if (dataProtectionProvider != null)
    {
        manager.UserTokenProvider =
                new DataProtectorTokenProvider<AppUser>(dataProtectionProvider.Create("ASP.NET Identity"));
    }
    return manager;
}

BEARBEITEN

1. Wichtiger Hinweis: Das direkte Öffnen des Endpunkts (localhost: 82 / token) funktioniert.

2. Das Aufrufen der API (localhost: 82 / api / ..) aus dem Webprojekt funktioniert ebenfalls, sodass CORS für WebApi aktiviert ist.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage