简体   繁体   中英

about forms authentication and redirect

Every time I try to Response.Redirect("tothepageIwant.aspx"); tt takes me to ~/Account/Logon.aspx

Why is this happening? I'm using Forms Authentication, with a custom method of authenticating, using PrincipalContext.ValidateCredentials .

If the credentials are valid, I want to Redirect.Response to the page I'm allowing the user to reach.

Instead, anytime I successfully login, it redirects me to the old Account/Logon.aspx .

Any suggestions? Anything I need to look out for when using Forms Authentication with custom method of authenticating?

EDIT (add code):

    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;
    }
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;
}

If you want to check and redirect.. store the value inside a session variable inside the Global.asax

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

On the Page_Load of your login page assign the value if the code above succeeds

   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;
        }
    }
}

use the debugger and inspect all of the user. Properties ok

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM