【问题标题】:about forms authentication and redirect关于表单身份验证和重定向
【发布时间】:2015-02-02 19:35:18
【问题描述】:

每次我尝试Response.Redirect("tothepageIwant.aspx"); tt 都会将我带到~/Account/Logon.aspx

为什么会这样?我正在使用表单身份验证,使用自定义身份验证方法,使用 PrincipalContext.ValidateCredentials

如果凭据有效,我想重定向。响应我允许用户访问的页面。

相反,只要我成功登录,它就会将我重定向到旧的Account/Logon.aspx

有什么建议吗?使用带有自定义身份验证方法的表单身份验证时我需要注意什么?

编辑(添加代码):

    protected void Submit1_Click(object sender, EventArgs e)
    {
        var auth = new AuthClass();
        var result = auth.ValidateCredentials(UserEmail.Text, UserPass.Text);
        if (result)
        {
            Response.Redirect("~/Members/RollReport.aspx");
        }
        else
        {
            Msg.Text = "Not authorized to access this page.";
        }
    }

    public bool ValidateCredentials(string user, string pass)
    {
        using (var pc = new PrincipalContext(ContextType.Domain, "Domain.name"))
        {
            // validate the credentials
            try
            {
                var isValid = pc.ValidateCredentials(user, pass);
                if (isValid)
                {
                    var isAuth = AuthorizeUser(user);
                    return isAuth;
                }
                else
                {
                    return false;
                }
            }
            catch (ActiveDirectoryOperationException)
            {
                throw;
            }
        }
    }

    private bool AuthorizeUser(string user)
    {
        var isAuth = false;
        var authList = (List<string>)HttpContext.Current.Cache["AuthList"];
        foreach (var id in authList)
        {
            if (id == user)
            {
                isAuth = true;
            }
        }
        return isAuth;
    }

【问题讨论】:

  • 它与一个或两个 Page_Load 事件中的代码逻辑有关......也许显示一些代码将帮助我们了解您哪里出错了......您可以使用 Session 更轻松地做到这一点您在 Global.asax 文件内的 OnSessionStart() 事件中初始化的变量以及正确使用 PrincipalContext`..
  • 我用认证方法代码编辑过。
  • 我不一定有身份验证问题。老实说,我不确定将表单身份验证与我的自定义方法一起使用是否正确。
  • 如果您要针对AD / LDAP 进行验证,您将变得太复杂了,您可以使用我发布的示例来完成所有这些操作,无论是否使用 FormsAuthentication,我都会一直使用它...
  • 这可能是我需要研究的。我很感激帮助。非常感谢。

标签: c# asp.net webforms forms-authentication


【解决方案1】:
var userName = Request.ServerVariables["LOGON_USER"];//or some other method of capturing the value from the username
var pc = new PrincipalContext(ContextType.Domain);
var userFind = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, userName);
if(userFind != null)
{
   HttpContext.Current.Session["username"] = userFind.DisplayName;
}

如果你想检查和重定向.. 将值存储在Global.asax 内的会话变量中

protected void Session_Start(object sender, EventArgs e)
{
   //declare and Initialize your LogIn Session variable
   HttpContext.Current.Session["username"] = string.Empty;
}

如果上面的代码成功,则在登录页面的 Page_Load 上赋值

   if(HttpContext.Current.Session["username"] == null)
    {
       //Force them to redirect to the login page 
    }
    else
    {
        Response.Redirect("tothepageIwant.aspx");   
    }

if you want to do the same thing inside a using(){} statement

string fullName = null;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    using (UserPrincipal user = UserPrincipal.FindByIdentity(context,"yourusernamehere")) //User.Identity.Name
    {
        if (user != null)
        {
            fullName = user.DisplayName;
        }
    }
}

使用调试器并检查所有user. Properties ok

【讨论】:

  • 我试了一下。现在,有什么我没有做的吗?我初始化 Session["username"] 并且当 Response.Redirect 时,会话将作为“”出现。
  • HttpContext.Current.Session vs Session["username"] 之间存在差异,您能否举个例子说明您如何传递用户名.. 是用户名还是域\用户名?
  • 可以通过对用户名进行编码来单独测试代码中的前 3 行。查看userFind 变量是否返回预期值非常简单。
  • 啊,我明白了,这是有道理的。再次感谢您的时间和帮助。
  • 我将在 using 声明中发布一个更简单/紧凑的方式来做同样的事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多