C# 响应。重定向失败,但可用于刷新IIS Express VS2013
我在Visual Studio 2013中有一个ASP.NET项目,在IIExpress(版本8.08418.0)中本地运行以进行测试 主页(adminDefault.aspx)将用户重定向到Login.aspx以进行身份验证(这是基于作为url变量传递给adminDefault.aspx的用户ID以静默方式完成的)。成功验证后,登录页面将其发送回adminDefault.aspx。然后默认将数据加载到Gridview中 整个过程大约需要15秒 当我从Visual Studio 2013运行此程序时,在大约4秒钟内出现错误“此页面无法显示”。但如果我在工作一段时间后手动点击刷新,一切都会很好 在我的代码中,由于怀疑超时,我隐式地设置了足够长的时间来运行所有内容:C# 响应。重定向失败,但可用于刷新IIS Express VS2013,c#,asp.net,visual-studio-2013,iis-express,C#,Asp.net,Visual Studio 2013,Iis Express,我在Visual Studio 2013中有一个ASP.NET项目,在IIExpress(版本8.08418.0)中本地运行以进行测试 主页(adminDefault.aspx)将用户重定向到Login.aspx以进行身份验证(这是基于作为url变量传递给adminDefault.aspx的用户ID以静默方式完成的)。成功验证后,登录页面将其发送回adminDefault.aspx。然后默认将数据加载到Gridview中 整个过程大约需要15秒 当我从Visual Studio 2013运行此程
if (!this.IsPostBack)
{
Session.Timeout = 120; // seconds before timeout
try
{
// OnUser should have been set from Login.asp. If it is null, send to Login
MembershipUser onUser = Membership.GetUser();
if (onUser == null)
{
Response.Redirect("/login.aspx", true);
}
else
{
String currentUserName = Membership.GetUser().UserName;
userRoles = Roles.GetRolesForUser(Membership.GetUser().UserName);
}
}
即使忽略Session.Timeout,默认值也是20秒,因此我希望它仍然可以工作
因此:
1) 为什么第一次贯穿失败?
2) 有什么办法可以预防吗?如果失败,我可以捕获未找到的页面并自动刷新吗
编辑:这与登录到Login.aspx并返回的过程有关。如果我删除Response.Redirect(“/login.aspx”,true)代码>
然后这就如预期的那样起作用了。问题是,我需要Login.aspx进行验证。任何人都知道为什么登录和返回的过程不起作用。。。那么刷新后呢
当我试着在Chrome上运行它时,我并没有走得更远。Chrome向我展示了这一点:
点击此处的只需循环片刻,然后返回屏幕
IE上的开发者工具在这种情况下没有帮助(至少我可以看到)。当我在调试器中运行我的项目时,它会启动一个新的IE会话。在打开开发人员工具并按“播放”录制事件之前,会话给了我“无法显示页面”错误。当然,我可以在工具中点击“播放”,然后刷新,但当然,所有操作都会运行,不会出错。刷新使一切正常运行。我需要在那之前找出它失败的原因
编辑:这是Login.aspx和Web.Security代码。我没有写,但它看起来像样板:
protected void Page_Load(object sender, EventArgs e)
{
string userName = "";
bool authenticated = FormsAuthentication.Authenticate(ref userName);
if (authenticated)
{
FormsAuthentication.RedirectFromLoginPage(userName, false);
}
}
/// <summary>
/// Validates a user based on the session id found in the ReturnURL against credentials stored in the ASP.NET membership.
/// </summary>
/// <param name="userName">The user name.</param>
/// <returns>true if the user name and password are valid; otherwise, false.</returns>
public static bool Authenticate(ref string userName)
{
bool Authenticated = Authenticate(ref userName, GetSessionId());
return Authenticated;
}
/// <summary>
/// Redirects an authenticated user back to the originally requested URL or the default URL using the specified cookie path for the forms-authentication cookie.
/// </summary>
/// <param name="userName">The authenticated user name. </param>
/// <param name="createPersistentCookie">true to create a durable cookie (one that is saved across browser sessions); otherwise, false. </param>
/// <param name="strCookiePath">The cookie path for the forms-authentication ticket. </param>
public static void RedirectFromLoginPage(string userName, bool createPersistentCookie, string strCookiePath)
{
System.Web.Security.FormsAuthentication.SetAuthCookie(userName, false, strCookiePath);
// Redirect back to request page.
HttpContext.Current.Response.Redirect(GetRedirectUrl());
}
验证代码如下所示:
protected void Page_Load(object sender, EventArgs e)
{
string userName = "";
bool authenticated = FormsAuthentication.Authenticate(ref userName);
if (authenticated)
{
FormsAuthentication.RedirectFromLoginPage(userName, false);
}
}
/// <summary>
/// Validates a user based on the session id found in the ReturnURL against credentials stored in the ASP.NET membership.
/// </summary>
/// <param name="userName">The user name.</param>
/// <returns>true if the user name and password are valid; otherwise, false.</returns>
public static bool Authenticate(ref string userName)
{
bool Authenticated = Authenticate(ref userName, GetSessionId());
return Authenticated;
}
/// <summary>
/// Redirects an authenticated user back to the originally requested URL or the default URL using the specified cookie path for the forms-authentication cookie.
/// </summary>
/// <param name="userName">The authenticated user name. </param>
/// <param name="createPersistentCookie">true to create a durable cookie (one that is saved across browser sessions); otherwise, false. </param>
/// <param name="strCookiePath">The cookie path for the forms-authentication ticket. </param>
public static void RedirectFromLoginPage(string userName, bool createPersistentCookie, string strCookiePath)
{
System.Web.Security.FormsAuthentication.SetAuthCookie(userName, false, strCookiePath);
// Redirect back to request page.
HttpContext.Current.Response.Redirect(GetRedirectUrl());
}
//
///根据在ReturnURL中找到的会话id和存储在ASP.NET成员身份中的凭据验证用户。
///
///用户名。
///如果用户名和密码有效,则为true;否则,错误。
公共静态bool身份验证(参考字符串用户名)
{
bool Authenticated=Authenticate(ref userName,GetSessionId());
返回认证;
}
///
///使用表单身份验证cookie的指定cookie路径将经过身份验证的用户重定向回最初请求的URL或默认URL。
///
///已验证的用户名。
///true创建持久cookie(跨浏览器会话保存的cookie);否则,错误。
///表单身份验证票证的cookie路径。
公共静态void RedirectFromLoginPage(字符串用户名、bool createPersistentCookie、字符串strCookiePath)
{
System.Web.Security.FormsAuthentication.SetAuthCookie(用户名,false,strCookiePath);
//重定向回请求页面。
HttpContext.Current.Response.Redirect(GetRedirectUrl());
}
我认为在您的初始陈述中,假设每个人都已登录或未登录。但未登录的用户和访问的人也可以是匿名的(?)。您要做的是保护此admin.Default页面,并且只允许授权用户。当前,如果用户=null或获取角色,则您的2个条件是将用户发送回login.aspx页面(它们当前处于打开状态)。
身份验证是关于知道用户是谁,而授权则是关于对资源的访问。您只希望获得授权的用户访问该资源
我会将admin.Default.aspx、admin.Default.cs和admin.Default.designer.cs放在它们自己的名为AdminSecure的文件夹中。然后在文件夹中添加它们之后,右键单击并添加一个Web.config文件。在该Web.config中,您需要做的是拒绝匿名用户。因此,当他们尝试访问AdminDefault页面时,会自动重定向
//拒绝非登录用户?==匿名的
<xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
//this can allow particular users
<allow users="User1@mail.com"/>
//deny everyone but one user:
<allow users="User1@mail.com"/>
<deny users="*"/>
//这可以允许特定用户使用
//拒绝除一个用户以外的所有用户:
注释掉您当前的代码,希望这有所帮助。根据注释中的应用程序情况,您无法通过web.config管理身份验证/授权。而且,Membership
类通常是可以访问的,如果您有自定义的提供者,这不是访问登录用户的默认方式。因此,请执行以下任一操作:
按以下方式检查if
语句中的登录用户:
if (User.Identity.IsAuthenticated)
{
Response.Redirect("/login.aspx", true);
}
else
{
String currentUserName = Use User.Identity.Name; // accessing the logged in uer
userRoles = Roles.GetRolesForUser(currentUserName); // not sure about this step, have you added your roles to web.Config? or from where you get them
}
根据您的ERP与应用程序的集成方式(您应该使用自定义提供商),检查用户在何处获得身份验证。(例如,谁在调用Membership.ValidateUser(strUsername,strPassword)
。并确保您的login.aspx页面正在更新会话状态
检查有关实现自定义提供程序的详细信息如果Login.aspx在将会话持久化到浏览器之前重定向到adminDefault.aspx,则将发生无限重定向循环
我的线索是:
1) 由于您已经有一个重定向循环——虽然不是有意无限的——在未处理的情况下,它有变成无限的风险
2) 您说可能发生了超时,但服务器端超时被忽略了:这让我认为这种情况发生在客户端,实际上可能是您的浏览器作为安全措施打破了重定向循环
3) 刷新帮助:这会在会话被持久化后中断重定向循环