[ASP.NET]Form인증을 통한 로그인 구현 학습/ASP.NET2014. 7. 7. 18:13
Form인증 없어도 들어갈수있게 하는방법은
<system.web>밖에
<location path="MainForm.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
이렇게 작성하면 MainForm.aspx페이지는 폼인증 없이도 들어갈수가있다.
그리고 Login페이지를 만들어준다.
<head>
<title>로그인하기폼</title>
<script runat="Server">
void Login_Click(object sender, EventArgs e)
{
if ((UserId.Text == "admin") && (UserPw.Text == "admin"))
{
FormsAuthentication.RedirectFromLoginPage(UserId.Text, false);
Response.Redirect("Today.aspx");
}
else
{
Msg.Text = "ID 또는 PW가 틀립니다. 다시시도해주세요";
}
}
</script>
</head>
<body>
<h3 align="center" style="margin-right:170px">
Login Page</h3>
<table align="center">
<tr>
<td>
ID :
</td>
<td>
<asp:TextBox ID="UserId" runat="Server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="UserId"
Display="Dynamic" ErrorMessage="ID는 빈값이 들어갈수 없습니다." runat="Server"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
PW :
</td>
<td>
<asp:TextBox ID="UserPw" TextMode="Password" style="margin-top:5px" runat="Server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="UserPw"
ErrorMessage="PW는 빈값이 들어갈수 없습니다." runat="Server"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td align="right">
<asp:Button ID="Submit1" OnClick="Login_Click" Text="로그인" style="margin-top:5px" runat="Server" />
</td>
<td>
</td>
</tr>
</table>
<p align="center">
<asp:Label ID="Msg" ForeColor="Red" runat="Server"></asp:Label>
</p>
</body>
로그인 페이지는 디자인폼에서 이렇게 설정해주면된다.
만약 폼인증 없이 들어가는 부분에 보여서는 안될 버튼들이 있다면
protected void Page_Load(object sender, EventArgs e)
{
if (this.User.Identity.IsAuthenticated) //로그인이 되어있다면.
{
FileUpload1.Visible = true; //FileUpload컨트롤을 보여준다.
btn_Upload.Visible = true; //btn_Upload버튼을 보여준다.
}
else //로그인이 안되어있다면
{
FileUpload1.Visible = false; //FileUpload컨트롤을 보여주지않는다.
btn_Upload.Visible = false; //btn_Upload버튼을 보여주지않는다.
}
}
폼 로드 부분에 이런식으로 셋팅을 해주면 폼인증 하였을때만 보여지게 된다.
로그아웃은
<head runat="server">
<title></title>
<script runat="Server">
void Signout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("Today.aspx");
}
</script>
</head>
자바스트립트로 구현을 하여 head부분에 추가시켜주고
body부분에 버튼을 추가시켜주면된다.
<asp:Button ID="btn_logout" OnClick="Signout_Click" runat="Server" Text="Logout" />
'학습 > ASP.NET' 카테고리의 다른 글
[ASP.NET] GridView 데이터 Excel로 만들기 (0) | 2014.10.30 |
---|---|
[ASP.NET] behind code에서 html 태그 컨트롤 하기 (0) | 2014.10.13 |
[ASP.NET]GridView DataformatString (0) | 2014.10.06 |
MVC패턴 개념(Model-View-Controller) (0) | 2014.05.15 |