Ant*_*nek 4 authentication cookies middleware owin openid-connect
我正在尝试将OpenId Connect集成到长期存在的Webforms应用程序中。我能够迁移该应用程序以使用OWIN,并且正在使用OpenIdConnectAuthenticationMiddleware对我的IdP提供程序进行身份验证。一切正常,直到需要构造从IdP获得的新身份并设置Cookie为止(我认为这没有发生)。
我的Startup.Configure方法的重要部分:
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/login.aspx"),
CookieManager = new SystemWebCookieManager() //custom cookie manager
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://[development_domain]/core",
ClientId = "VDWeb",
ResponseType = "code id_token token",
Scope = "openid profile",
UseTokenLifetime = true,
SignInAsAuthenticationType = "Cookies",
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = async n =>
{
var userInfo = await EndpointAndTokenHelper.CallUserInfoEndpoint(n.ProtocolMessage.AccessToken);
//now store Preferred name :
var prefNameClaim = new Claim(
Thinktecture.IdentityModel.Client.JwtClaimTypes.PreferredUserName,
userInfo.Value<string>("preferred_username"));
var myIdentity = new ClaimsIdentity(
n.AuthenticationTicket.Identity.AuthenticationType,
Thinktecture.IdentityModel.Client.JwtClaimTypes.PreferredUserName,
Thinktecture.IdentityModel.Client.JwtClaimTypes.Role);
myIdentity.AddClaim(prefNameClaim);
//add unique_user_key claim
var subjectClaim = n.AuthenticationTicket.Identity.FindFirst(Thinktecture.IdentityModel.Client.JwtClaimTypes.Subject);
myIdentity.AddClaim(new Claim("unique_user_key", subjectClaim.Value));
myIdentity.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
var ticket = new AuthenticationTicket(myIdentity, n.AuthenticationTicket.Properties);
var currentUtc = new SystemClock().UtcNow;
ticket.Properties.IssuedUtc = currentUtc;
ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromHours(12));
n.AuthenticationTicket = ticket;
},
}
});
Run Code Online (Sandbox Code Playgroud)
我可以确认AuthentocationTicket已正确填充,但未设置auth cookie。我确实知道此问题,网址为https://katanaproject.codeplex.com/workitem/197,我已经尝试了针对此问题提供的所有变通办法,但没有任何帮助。有趣的是,当我尝试将自己的cookie放入SecurityTokenValidated事件-中时n.Response.Cookies.Append("Test", "Test");
,我可以看到cookie设置正确。
解决方法之一建议实现自己的CookieManager。令我感到好奇的是,当我在此自定义管理器中的cookie设置器中放置一个断点时,它没有命中,即中间件似乎甚至没有尝试设置cookie。所以我有一个主要问题-中间件在什么时候将尝试设置cookie?设置AuthenticationTicket时是否可以?
编辑1:添加更多信息。我试图与另一个Web应用程序进行比较,这次是MVC,我将其配置为使用相同的IdP,并且可以按预期工作。两个应用程序的启动代码是相同的。通过SecurityTokenValidated事件进行调试时,我可以看到MVC应用程序(正在运行)已创建了System.Security.Principal.WindowsPrincipal
身份,而Webforms应用程序(正在运行)已创建了System.Security.Principal.GenericIdentity
身份。
我还添加了这个小片段
app.UseStageMarker(PipelineStage.Authenticate);
app.Use((context, next) =>
{
var identity = context.Request.User.Identity;
return next.Invoke();
});
Run Code Online (Sandbox Code Playgroud)
只是为了了解在此管道阶段填充了什么身份。对于MVC应用程序(正在运行),我看到通过设置AuthenticationTicket添加的身份,对于Webforms应用程序,我仍然看到未经身份验证的GenericIdentity。
好的,这很令人尴尬-问题出在CookieAuthenticationOptions中,显然AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
与并不相同AuthenticationType = "Cookies"
。一旦以这种方式设置,它就可以正常工作。
归档时间: |
|
查看次数: |
2481 次 |
最近记录: |