我尝试了以下方法仅重定向到loginpage.aspx,但是有一个问题 [英] I tried the following way to redirecting to loginpage.aspx only, but there is a problem

查看:150
本文介绍了我尝试了以下方法仅重定向到loginpage.aspx,但是有一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



在我的网络应用程序中,我有两页。



1 .LoginPage.aspx

2.MainPage.aspx



登录页面使用的控件

用户名文本框

密码文本框

按钮


运行时的

 http: //   localhost / WebApplication3 / LoginPage.aspx  



然后是按钮上的身份验证成功点击我们重定向到

 Response.Redirect( 〜/ MainPage.aspx); 





如果您使用以下网址,则无需身份验证

 http: //   localhost / WebApplication3 / MainPage.aspx  



它去了mainpage.aspx



我的问题是如果使用这个URL应该是redirecte d仅限登录页面。



我在谷歌搜索了很多例子。但是我没有得到明确的解决方案。

请解释我实现了这个目标。



我有母版页,我试过,



1.在我的web.config文件

< authentication mode =  形式> 
< forms loginUrl = Login.aspx timeout = 1 />
< / 身份验证 >
< authorization>
< deny users = />
< / 授权 >



2.和我的登录文件,我们有很多图像和标题然后我的按钮点击事件

 受保护  void  Login_Click( object  sender, EventArgs e)
{
if ((UserEmail.Value == < span class =code-string> 12)&&(UserPass.Value == 12))
{
FormsAuthentication.RedirectFromLoginPage(UserEmail.Value, true );
Response.Redirect( MainPage.aspx);
}
else
{
Msg.Text = 凭证无效:请重试;
}
}



3.我的WebForm1文件就像一次注销按钮点击事件。

< pre lang =c#> FormsAuthentication.SignOut();
Response.Redirect( Login.aspx);





如果我运行此应用程序,我的设计已更改,并且图像未显示在浏览器上。



如果我在web.config文件中使用这个部分

< ;授权> 
< deny users = />
< / 授权 >





设计和图片显示不正确但我可以在浏览器中键入其他URL时重定向登录页面,如果我删除此标签设计是好的,但我无法重定向到浏览器中键入其他URL时登录页面。

解决方案

在您的网络配置中尝试这个



 <  身份验证    mode   = 表单 >  
< 表格 loginUrl = 登录.aspx 名称 = .ASPXFORMSAUTH / >
< / authentication >
< 授权 >
< deny 用户 = / >
< span class =code-keyword>< / authorization >


在登录页面中调用Response.Redirect之前,请添加:

 FormsAuthentication.SetAuthCookie( 用户名 true ); 



在MainPage.aspx中试试这个:

  if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Response.Redirect( Login.aspx);
}









--Amit


创建会话变量,并在用户登录Loginpage.aspx之前将其设置为true,然后重定向到mainpage.aspx

会话[  LoggedIn] =  true  


主页上的
检查上述会话是否属实,如果错误则将用户重定向回登录页面

< pre lang =c#> if (会话[ LoggedIn] == true
{
// 继续
}
其他
{
回复。重定向( 〜/ LoginPage.aspx);
}





确保在用户注销时将会话变量设置为false。


Hi experts,

In my Web application, I am having two pages.

1.LoginPage.aspx
2.MainPage.aspx

controls used in Loginpage
username text box
Password text box
Button

while running

http://localhost/WebApplication3/LoginPage.aspx


then the authentication success on the button click we redirecting to

Response.Redirect("~/MainPage.aspx");



without authentication if you use the below URL

http://localhost/WebApplication3/MainPage.aspx


its going to mainpage.aspx

my problem is if use this URL should be redirected to LoginPage only.

I have searched many examples in Google.But I haven''t get the clear solution.
kindly explain me to achieve this.

I have master page and I tried like,

1. in my web.config file

<authentication mode="Forms">
      <forms loginUrl="Login.aspx" timeout="1" />
    </authentication>
                        <authorization>
				<deny users="?" />
			</authorization>


2. and my Login file, we have many images and headers then my button click event

protected void Login_Click(object sender, EventArgs e)
       {
           if ((UserEmail.Value == "12") && (UserPass.Value == "12"))
           {
               FormsAuthentication.RedirectFromLoginPage(UserEmail.Value, true);
               Response.Redirect("MainPage.aspx");
           }
           else
           {
               Msg.Text = "Invalid Credentials: Please try again";
           }
       }


3. My WebForm1 file like one logout button click event.

FormsAuthentication.SignOut();
            Response.Redirect("Login.aspx");



If i run this application my design changed and images were not displayed on the browser.

if i use this part in web.config file

<authorization>
				<deny users="?" />
			</authorization>



design and images were not displayed properly but i am able to redirect login page when other URL typed in the browser, if i remove this tag design is ok but i am unable to redirect to login page when other URL typed in the browser.

解决方案

Try this in your web config

<authentication mode="Forms">
     <forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH"/>
   </authentication>
   <authorization>
     <deny users="?"/>
   </authorization>


Before calling Response.Redirect in your login page add this:

FormsAuthentication.SetAuthCookie("username", true);


Try this in your MainPage.aspx:

if(!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
    Response.Redirect("Login.aspx");
}





--Amit


Create a Session Variable and set it to true when ever user is logged in on the Loginpage.aspx before redirecting to mainpage.aspx

Session["LoggedIn"] = true


on the mainpage.aspx Check if the above session vairable is true and the redirect the user back to login page if it is false

if (Session["LoggedIn"] == true)
{
//Continue
}
else
{
Response.Redirect("~/LoginPage.aspx");
}



Make sure to set the session variable back to false when user is logged off.


这篇关于我尝试了以下方法仅重定向到loginpage.aspx,但是有一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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