在Web应用程序中的母版页之前登录 [英] Log in before master page in web application

查看:61
本文介绍了在Web应用程序中的母版页之前登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用下面的代码时,login.aspx不会先运行!!



protected void Button1_Click(object sender,EventArgs e)

{



if((arch.Users.Any(u => u.username == TextBox1.Text))== true)

{

var get_user_pass =(来自arch.Users中的x,其中x.username == TextBox1.Text

选择x.password).FirstOrDefault();

// if((TextBox1.Text ==1)&&(TextBox2.Text ==1))

if(Convert.ToString(get_user_pass)= = TextBox2.Text)

{

FormsAuthentication.SetAuthCookie(TextBox1.Text,false);

string returnUrl = Request.QueryString [returnUrl ];

if(!string.IsNullOrEmpty(returnUrl))

{Response.Redirect(returnUrl); }

else {Response.Redirect(Default.aspx); }





}

}

}





我想要的是让我的defualt.aspx(在母版页内)作为应用程序的起始页面,但login.aspx必须有效,所以用户必须在使用母版页之前登录

when i use the code below login.aspx not run first !!

protected void Button1_Click(object sender, EventArgs e)
{

if(( arch.Users.Any(u=>u.username == TextBox1.Text))==true)
{
var get_user_pass= (from x in arch.Users where x.username==TextBox1.Text
select x.password).FirstOrDefault();
//if ((TextBox1.Text == "1") && (TextBox2.Text == "1"))
if(Convert.ToString(get_user_pass)==TextBox2.Text)
{
FormsAuthentication.SetAuthCookie(TextBox1.Text, false);
string returnUrl = Request.QueryString["returnUrl"];
if (!string.IsNullOrEmpty(returnUrl))
{ Response.Redirect(returnUrl); }
else { Response.Redirect("Default.aspx"); }


}
}
}


what i want is to make my defualt.aspx(which is inside master page) as start page of application, but login.aspx must in force so user must login before using the master page

推荐答案

使用FormsAuthentication并将LoginUrl设置为Login.aspx页面。类似于:



Use FormsAuthentication and set the LoginUrl to your Login.aspx page. Something like:

<authentication mode="Forms">
    <forms loginUrl="~/Login.aspx" protection="All" timeout="600" slidingExpiration="true" domain=""/>
  </authentication>





然后,他们登录后使用FormsAuthentication.RedirectFromLoginPage()设置身份验证cookie。



这样任何对您网站的请求如果未经过身份验证,都会自动发送到Login.aspx。



Then after they login set the authentication cookies using FormsAuthentication.RedirectFromLoginPage().

This way any request to your site if they have not been authenticated they will be sent automatically to Login.aspx.


这篇关于在Web应用程序中的母版页之前登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆