使用Owin cookie身份验证时遇到一个奇怪的问题.
当我启动我的IIS服务器时,身份验证在IE/Firefox和Chrome上运行得非常好.
我开始使用身份验证进行一些测试并在不同的平台上登录,我想出了一个奇怪的错误.偶尔Owin框架/ IIS只是不向浏览器发送任何cookie.我将输入一个用户名和密码,该代码运行正确,但根本没有cookie传递给浏览器.如果我重新启动服务器它开始工作,那么在某些时候我将尝试登录并再次cookie停止交付.单步执行代码不会做任何事情并且不会抛出任何错误.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
CookieHttpOnly = true,
AuthenticationType = "ABC",
LoginPath = new PathString("/Account/Login"),
CookiePath = "/",
CookieName = "ABC",
Provider = new CookieAuthenticationProvider
{
OnApplyRedirect = ctx =>
{
if (!IsAjaxRequest(ctx.Request))
{
ctx.Response.Redirect(ctx.RedirectUri);
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
在我的登录程序中,我有以下代码:
IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var authentication = HttpContext.Current.GetOwinContext().Authentication;
var identity = new ClaimsIdentity("ABC");
identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.User_ID.ToString()));
identity.AddClaim(new Claim(ClaimTypes.Role, role.myRole.ToString()));
authentication.AuthenticationResponseGrant =
new AuthenticationResponseGrant(identity, new AuthenticationProperties()
{
IsPersistent = isPersistent
}); …Run Code Online (Sandbox Code Playgroud) 我在使用WsFederation AuthenticationMVC Web应用程序时遇到登录循环问题.我用Visual Studio来创建Web应用程序的脚手架和来设置WsFederation的Startup.cs.这会生成以下代码块:
public class Startup
{
private static string realm = ConfigurationManager.AppSettings["ida:Wtrealm"];
private static string adfsMetadata = ConfigurationManager.AppSettings["ida:ADFSMetadata"];
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
{
Wtrealm = realm,
MetadataAddress = adfsMetadata
});
}
}
Run Code Online (Sandbox Code Playgroud)
Web应用程序托管在Azure中,ADFS位于内部.
在某些客户端上,当进行登录尝试时,登录页面进入循环,请求新令牌在ADFS服务器上导致以下异常:
异常详细信息:Microsoft.IdentityServer.Web.InvalidRequestException:MSIS7042:同一客户端浏览器会话在最后"7"秒内发出了"6"个请求.请联系您的管理员了解详情
我已经阅读了很多关于StackOverflow的文章,并查看了编写IdentityServer的人提供的各种示例,我尝试了各种配置选项,我无法将问题隔离到特定区域.
从我读到的内容来看,OWIN中间件丢失了对象的上下文,因此令牌"丢失".
我试图实现其他一些在StackOverflow上提供的示例代码但是,我似乎找不到解决方案来解决我的问题或者可能没有正确实现代码.
有任何想法吗?