OWIN 无效的 URI:Uri 字符串太长
OWIN Invalid URI: The Uri String is too long
我有一个托管在服务器 (IIS) 上的 MVC 应用程序,它指向 3 SQL 数据库。这已经 运行 几个月没有问题了。
我不得不更改所有 3 个 SQL 数据库的连接字符串以指向新数据库。
现在当我尝试登录时出现以下错误..
连接字符串正在使用 Windows 身份验证,此帐户在 AppPool 中设置。我还手动尝试使用该帐户连接到每个数据库实例,这工作正常。我开始认为更改是 SQL 连接只是一个转移注意力的问题。
关于错误消息,我完全理解错误是什么我只是不确定为什么会抛出它。我唯一能想到的是我处于某种重定向循环中,它附加了 URL.
感觉确实像是 IIS 问题,但我无法确定。
有没有人在使用 OWIN 之前遇到过这个问题,或者可以就可能诊断问题的调试步骤提出建议?
Startup.cs
public partial class Startup
{
private static bool IsAjaxRequest(IOwinRequest request)
{
IReadableStringCollection query = request.Query;
if ((query != null) && (query["X-Requested-With"] == "XMLHttpRequest"))
{
return true;
}
IHeaderDictionary headers = request.Headers;
return ((headers != null) && (headers["X-Requested-With"] == "XMLHttpRequest"));
}
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and role manager to use a single instance per request
app.CreatePerOwinContext(ParentDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.CreatePerOwinContext(PrincipalManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity =
SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(
TimeSpan.FromMinutes(int.Parse(WebConfigurationManager.AppSettings["RefreshInterval"])),
(manager, user) => manager.GenerateUserIdentityAsync(user),
claim => new Guid(claim.GetUserId())),
OnApplyRedirect = ctx =>
{
if (!IsAjaxRequest(ctx.Request))
{
ctx.Response.Redirect(ctx.RedirectUri);
}
}
}
});
}
}
最可能的原因是您陷入了错误循环。如果对存储用户的数据库的身份验证失败,那么您将被发送到错误页面,该页面将再次尝试 运行 身份验证并失败,并一次又一次地将您发送到错误页面。每次通过它都会附加到之前的 url 最终达到此状态。
经过几个小时的调查,我终于找到了问题所在。
问题是为用户添加的声明数量。一旦我们减少了索赔数量,它又开始工作了。
我有一个托管在服务器 (IIS) 上的 MVC 应用程序,它指向 3 SQL 数据库。这已经 运行 几个月没有问题了。
我不得不更改所有 3 个 SQL 数据库的连接字符串以指向新数据库。
现在当我尝试登录时出现以下错误..
连接字符串正在使用 Windows 身份验证,此帐户在 AppPool 中设置。我还手动尝试使用该帐户连接到每个数据库实例,这工作正常。我开始认为更改是 SQL 连接只是一个转移注意力的问题。
关于错误消息,我完全理解错误是什么我只是不确定为什么会抛出它。我唯一能想到的是我处于某种重定向循环中,它附加了 URL.
感觉确实像是 IIS 问题,但我无法确定。
有没有人在使用 OWIN 之前遇到过这个问题,或者可以就可能诊断问题的调试步骤提出建议?
Startup.cs
public partial class Startup
{
private static bool IsAjaxRequest(IOwinRequest request)
{
IReadableStringCollection query = request.Query;
if ((query != null) && (query["X-Requested-With"] == "XMLHttpRequest"))
{
return true;
}
IHeaderDictionary headers = request.Headers;
return ((headers != null) && (headers["X-Requested-With"] == "XMLHttpRequest"));
}
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and role manager to use a single instance per request
app.CreatePerOwinContext(ParentDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.CreatePerOwinContext(PrincipalManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity =
SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(
TimeSpan.FromMinutes(int.Parse(WebConfigurationManager.AppSettings["RefreshInterval"])),
(manager, user) => manager.GenerateUserIdentityAsync(user),
claim => new Guid(claim.GetUserId())),
OnApplyRedirect = ctx =>
{
if (!IsAjaxRequest(ctx.Request))
{
ctx.Response.Redirect(ctx.RedirectUri);
}
}
}
});
}
}
最可能的原因是您陷入了错误循环。如果对存储用户的数据库的身份验证失败,那么您将被发送到错误页面,该页面将再次尝试 运行 身份验证并失败,并一次又一次地将您发送到错误页面。每次通过它都会附加到之前的 url 最终达到此状态。
经过几个小时的调查,我终于找到了问题所在。
问题是为用户添加的声明数量。一旦我们减少了索赔数量,它又开始工作了。