2015-07-13 179 views
0

我有一個ASP.NET身份站點和一個ASP.NET OData站點。
這兩個站點都啓用了CORS,並且這兩個站點都使用ASP.NET Identity CookieAuthentication。
當我在我的計算機上使用IIS(不是表達式)在本地執行這兩個站點時,AUTH cookie正在傳遞到OData站點的每個請求的標題中。
但是,當我將站點部署到生產IIS服務器時,在調用生產OData站點時,標題缺少AUTH cookie。
生產和我的本地IIS都有相同的域名和CORS設置爲允許所有。使用CORS和Cookie身份驗證的ASP.NET身份/ OData缺少身份驗證Cookie

的WebApiConfig有

cors = new Http.Cors.EnableCorsAttribute("*", "*", "*"); 
config.Enable(cors); 

有人問之前,是機器關鍵是站點之間的相同。

UPDATE
這似乎是一個CORS問題。
當兩個站點位於本地計算機上時,它們使用相同的主機名和域名,但是當站點位於生產服務器上時,它們具有不同的主機名和相同的域名。

回答

0

我終於得到了這個工作。
在ASP.NET身份的網站我有以下幾點:

// configure OAuth for login 
app.UseCookieAuthentication(new CookieAuthenticationOptions { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     Provider = new CookieAuthenticationProvider(), 
     LoginPath = new PathString("/Account/Login.aspx"), 
     CookieName = ".TESTAUTH", 
     CookieDomain = ".test.com", 
     CookieSecure = CookieSecureOption.Always 
    }); 

看來,在ASP.NET網站身份的重要組成部分是,「CookieName,CookieDomain,以及計算機密鑰」必須匹配一個在OData網站上。

,然後在現場的OData我有以下幾點:

// configure OAuth for login 
app.UseCookieAuthentication(new CookieAuthenticationOptions { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     Provider = new CookieAuthenticationProvider { OnApplyRedirect = ApplyRedirect }, 
     LoginPath = new PathString("/Account/Login.aspx"), 
     CookieName = ".TESTAUTH", 
     CookieDomain = ".test.com", 
     CookieSecure = CookieSecureOption.Always 
    }); 

// build the configuration for web api 
HttpConfiguration config = new HttpConfiguration(); 

// Enable CORS (Cross-Origin Resource Sharing) for JavaScript/AJAX calls 
// NOTE: USING ALL "*" IS NOT RECOMMENDED 
var cors = new Http.Cors.EnableCorsAttribute("*", "*", "*"); 
config.EnableCors(cors); 

// call the web api startup 
WebApiConfig.Register(config); 
app.UseWebApi(config); 

private void ApplyRedirect(CookieApplyRedirectContext context) 
{ 
    Uri absoluteUri = null; 
    if (Uri.TryCreate(context.RedirectUri, UriKind.Absolute, absoluteUri)) 
    { 
     var path = PathString.FromUriComponent(absoluteUri); 
     if (path == context.OwinContext.Request.PathBase + context.Options.LoginPath) 
     { 
      QueryString returnURI = new QueryString(context.Options.ReturnUrlParameter, context.Request.Uri.AbsoluteUri); 
      context.RedirectUri = "https://www.test.com/Account/Login.aspx" + returnURI.ToString; 
     } 
    } 
    context.Response.Redirect(context.RedirectUri); 
} 

即使它並不在現場的OData存在,你不能用一個完整的URL到其他網站上的「LOGINPATH」是必需的爲登錄路徑。
我使用「OnApplyRedirect」重定向到實際的登錄頁面。

我不確定「config.EnableCors」和「app.UseCors」之間有什麼區別,但EnableCors似乎現在正在工作。

0

您可能需要在OAuthAuthorizationServerProvider中指定「Access-Control-Allow-Origin」。

我正在使用OWIN,但你應該可以做類似的事情。

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 
0

嘗試在您的OWIN啓動類中添加策略,如下所示。請記住,Startup類可能有一些不同的類文件,因爲它是一個部分類。另外,請檢查ConfigureAuth方法以查看是否根據您的需要設置了所有內容。例如,您將Identity的外部登錄Cookie設置爲在ConfigureAuth方法中複製,以允許外部登錄Cookeies(如Facebook和Google)。

public void Configuration(IAppBuilder app) 
    { 
     // 
     app.UseCors(CorsOptions.AllowAll); 
     ConfigureAuth(app); 

    } 


     app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);