【发布时间】:2017-03-28 10:17:47
【问题描述】:
我正在使用带有 OWIN 启动类的 Umbraco 7.5。
尽管使用 cookie 身份验证存在缺点,但我正在尝试在 MVC 和 Web API 之间共享 cookie 身份验证。
我的 OWIN 启动课程中有这个:
private static void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
CookieSecureOption secureCookieOption = CookieSecureOption.SameAsRequest;
#if DEBUG
secureCookieOption = CookieSecureOption.Never;
#endif
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
AuthenticationMode = AuthenticationMode.Active,
LoginPath = new PathString("/Account/Login"),
CookieSecure = secureCookieOption,
CookieManager = new ChunkingCookieManager(),
Provider = new CookieAuthenticationProvider()
}, PipelineStage.Authenticate);
//configure B2C OAuth middleware
foreach (string policy in AppSettings.B2CPolicies)
{
app.UseOpenIdConnectAuthentication(CreateBearerOptionsFromPolicy(policy));
}
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
就 Umbraco 和自定义 MVC 页面而言,这可以正常工作 - 当前用户身份可用并且 Umbraco 辅助方法按预期工作。
但是,对于 Web API 控制器 - 无论是从 UmbracoApiController 还是仅从 ApiController 派生,HTTP 上下文上的当前用户身份始终为空。我已经检查了发送到 API 控制器的浏览器请求,并且包含了 ASPNET 身份 cookie,所以我很困惑为什么这不能转换为线程和 httpcontext 上的用户身份。任何人都可以对此有所了解吗?
编辑:有关此的更多信息-我尝试创建自己的自定义 cookie 认证中间件并替换了标准的 MS CookieAuthenticationHandler 和我的自定义实现,这样我 可以通过它跟踪调用。有趣的是,对于一个普通的 MVC 页面加载时调用 AuthenticateCoreAsync 方法, 成功读取cookie并返回有效身份验证 票。对于 Web API 调用,AuthenticateCoreAsync 方法不是 在调用 API 方法之前完全调用。
【问题讨论】:
标签: c# asp.net-web-api2 asp.net-identity owin umbraco7