简体   繁体   English

使用表单身份验证类进行身份验证

[英]Using forms authentication class for authentication

I am developing an user authentication site. 我正在开发一个用户认证站点。 I have a login page, "Login.aspx" in which i have provided a login control. 我有一个登录页面“ Login.aspx”,其中提供了一个登录控件。 In the web.config, 在web.config中,

<authentication mode="Forms">
        <forms name=".AuthenticationCookie" loginUrl="Login.aspx"  protection="All" timeout="60" path="/">
            <credentials passwordFormat="Clear">
                <user name="Jack" password="Jerry"/>
            </credentials>
        </forms>
    </authentication>
    <authorization>
        <deny users="*"/>
    </authorization>

In the login.aspx.cs page, I have provided, 在login.aspx.cs页面中,我提供了,

 protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        if (FormsAuthentication.Authenticate(Login1.UserName,Login1.Password))
        {
             FormsAuthentication.SetAuthCookie(Login1.UserName,true);
            Label1.Text = "Login Successful";
            Login1.InstructionText = "";
            FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);
            Response.Redirect("Success.aspx")

        }
        else
        {
            Label1.Text = "You are not an authentic user";
        }


    }
}

but however, while execution instead of going to success.aspx with the url http://localhost/Login.aspx?ReturnUrl=%2fSuccess.aspx 但是,但是,在执行而不是使用URL http://localhost/Login.aspx进入success.aspx ?ReturnUrl =%2fSuccess.aspx

Why is this so? 为什么会这样呢?

If you want to set the forms auth cookie yourself and redirect correctly based on the ReturnUrl query string parameter, you should look at the FormsAuthentication.RedirectFromLoginPage method. 如果要自己设置表单身份验证cookie并根据ReturnUrl查询字符串参数正确重定向,则应查看FormsAuthentication.RedirectFromLoginPage方法。 In your example, it would be: 在您的示例中,它将是:

FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);

That method sets the appropriate Forms auth cookie / ticket and then redirects based on the presence or absence of the ReturnUrl parameter. 该方法设置适当的Forms身份验证cookie /票证,然后根据ReturnUrl参数的存在或不存在进行重定向。 (If absent, it goes to the configured default page.) (如果不存在,它将转到已配置的默认页面。)

Hope this helps, 希望这可以帮助,

Donnie 唐尼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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