C# ASP.NET无法确定用户是否在代码隐藏之外登录
我在一个单独的类中有一个方法,我的代码隐藏调用该方法。方法如下:C# ASP.NET无法确定用户是否在代码隐藏之外登录,c#,asp.net,C#,Asp.net,我在一个单独的类中有一个方法,我的代码隐藏调用该方法。方法如下: public static int InsertCondition(Condition condition) { if (!HttpContext.Current.User.Identity.IsAuthenticated) { HttpContext.Current.Response.Redirect("~/Accounts/Logi
public static int InsertCondition(Condition condition)
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
HttpContext.Current.Response.Redirect("~/Accounts/Login.aspx", true);
}
try
{
var ConditionContext = new ConditionDataContext();
ConditionContext.Conditions.InsertOnSubmit(condition);
ConditionContext.SubmitChanges();
int condition_id = condition.ID;
return condition_id;
}
catch
{
return 0;
}
}
public static Guid GetCurrentUserGuid()
{
return (Guid)Membership.GetUser().ProviderUserKey;
}
由于某些原因,我在未登录时未被重定向。相反,执行try块,我在GetCurrentUserGuid函数中得到一个运行时错误,因为GetUser()对象未设置为实例。我明白这是因为我没有登录。这就是我试图通过检查用户身份是否经过身份验证来阻止的。我只是不明白为什么我没有被重定向
提前感谢。第二个代码块与第一个代码块如何匹配?我在任何地方都看不到引用。您可以调试应用程序并在InsertCondition上设置断点,然后查看表达式
HttpContext.Current.User.Identity.IsAuthenticated
?是真是假?