nỘi dung - aspnhatnghe.weebly.com5 authentication việc sử dụng chức năng login rất...
TRANSCRIPT
![Page 1: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/1.jpg)
1
![Page 2: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/2.jpg)
2
NỘI DUNG
Login Controls
Security
![Page 3: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/3.jpg)
3
Login Controls & MemberShip
LoginStatus Control
Login ->Trang đăng nhập (định nghĩa trong
web.config)
Logout ->Tự động
Từ chối truy xuất các tài nguyên được bảo vệ
-> trang đăng nhập
![Page 4: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/4.jpg)
4
Security
Không thể truy xuất các tài nguyên mật khi:
Chưa đăng nhập hoặc
Không thuộc vai trò phù hợp
Các bước
Tổ chức các tài nguyên bảo vệ
Gán quyền cho các user, role phù hợp
Định nghĩa trang đăng nhập sử dụng khi bị từ
chối
![Page 5: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/5.jpg)
5
Authentication
Việc sử dụng chức năng login rất thường phổ biến trong
ứng dụng web
Hạn chế user truy cập vào vùng an toàn
User đã đăng ký mới có quyền sử dụng các chức
năng như:
Post bài, download, viết comment…
Xây dựng ứng dụng web với chức năng login cũng không
quá khó khăn.
Từ việc cho phép user truy cập một trang nào đó,
hoặc những chỉ những chức năng nào đó của một
trang web
![Page 6: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/6.jpg)
6
Authentication
Các chức năng Authentication được ASP.NET hỗ trợ.
Forms Authentication thường được đề cập do tính tiện
dụng của nó.
Foms Authentication cho phép developer lưu trữ thông tin
xác thực như username, pass trong file web.config, hoặc
trong database, XML file, text file.
Điều quan trọng là form authentication sẽ làm những
công việc state-tracking cho developer.
![Page 7: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/7.jpg)
7
Web.config-Authentication
<roleManager enabled="true"/>
<authentication mode="Forms">
<forms name=“DN” loginUrl="DangNhap.aspx"/>
</authentication>
Authentication
Thuộc tính Mô tả
Name Tên của cookie sử dụng form authentication
LoginUrl URL khi user chưa đăng nhập sẽ được gởi đến
Protection Phương thức dùng để bảo vệ cookie
Timeout Thời gian để cookie hết hạn
![Page 8: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/8.jpg)
8
Form authentication
Credential
Thuộc tính Mô tả
PasswordFormat Định dạng cho password: clear, SHA1, MD5
User Lưu trữ thông tin về user name & pass
Authorization
Deny ! Allow Từ chối hoặc cho phép truy cập trang nào đó
![Page 9: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/9.jpg)
9
Web.config-Authorization (1)
Từ chối những user chưa đăng nhập
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
![Page 10: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/10.jpg)
10
Web.config-Authorization (2)
Chỉ chập nhận 2 user là admin và nhatnghe
<?xml version="1.0"?><configuration><system.web><authorization><allow users=“admin,nhatnghe" /><deny users="*" />
</authorization></system.web></configuration>
![Page 11: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/11.jpg)
11
Web.config-Authorization (3)
Chỉ chấp nhận các users thuộc vào 2 role là Administrator hoặc Collaborator.
<?xml version="1.0"?><configuration><system.web><authorization><allow roles="Administrator,Collaborator" /><deny users="*" />
</authorization></system.web></configuration>
![Page 12: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/12.jpg)
12
Web.Config
<configuration>
<system.web>
<customErrors mode="Off"/>
<authentication mode="Forms">
<forms name="appNameAuth" loginUrl="login.aspx" timeout="30">
<credentials passwordFormat="Clear">
<user name=“demo" password=“demo@123" />
<user name=“admin" password=“admin" />
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
Web.Config
Không cho user anonymous truy cập
![Page 13: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/13.jpg)
13
Minh họa sử dụng Forms Authen
Tạo một ứng dụng Web App đơn giản
Trang login.aspx: để đăng nhập
Trang SecurePage.aspx: là trang được bảo
vệ, chỉ có user có quyền mới có thể vào được.
File cấu hình Web.config để cấu hình Forms
Authentication
![Page 14: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/14.jpg)
14
Minh họa sử dụng Forms Authen
Trang login.aspx
protected void btnLogin_Click(object sender, EventArgs e)
{
if (FormsAuthentication.Authenticate(txtUserName.Text,txtPassword.Text))
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, true);
else
lblInfo.Text = "Đăng nhập không thành công!";
}
![Page 15: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/15.jpg)
15
Minh họa sử dụng Forms Authen
Trang SercurePage.aspx
Hiển thị thông tin user đăng nhập, chức năng
logout.
LoginName LoginStatus
![Page 16: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/16.jpg)
16
Minh họa sử dụng Forms Authen
File cấu hình Web.config
![Page 17: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/17.jpg)
17
Minh họa sử dụng Forms Authen
Chạy thử nghiệm
![Page 18: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/18.jpg)
18
Minh họa 2
Trong minh họa 1:
Thông tin username, password được lưu trong
file web.config
Tất cả các trang của ứng dụng đều phải yêu
cầu đăng nhập
Minh họa 2:
Thông tin username, password lưu bên ngoài
file config.
Cho một số trang yêu cầu phải đăng nhập.
![Page 19: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/19.jpg)
19
Minh họa 2
Tạo các trang
Index.aspx
Login.aspx
SecurePage.aspx
publicPage.aspx
Web.config
![Page 20: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/20.jpg)
20
Minh họa 2
Tạo trang login.aspx
protected bool CheckUser(string user, string pass)
{
// phần check user này ta có thể truy xuất database,
// file xml, file text để lấy thông tin account.
//Phần demo nên ta hardcode :D luôn!
if (user == “demo" && pass == “demo@123")
return true;
return false;
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (CheckUser(txtUserName.Text,txtPassword.Text))
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, true);
else
lblInfo.Text = "Đăng nhập không thành công!";
}
![Page 21: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/21.jpg)
21
Minh họa 2
Phần trang SecurePage.aspx tương tự minh họa
1
Tạo page mới đặt tên PublicPage.aspx
Trang này chỉ chứa dòng thông báo là “Trang
public”
<form id="form1" runat="server">
<div>
<h3>Trang này được bảo vệ</h3> <hr />
<asp:LoginName ID="LoginName1" runat="server" /> <br />
<asp:LoginStatus ID="LoginStatus1" runat="server" />
<br />
</div>
</form>
![Page 22: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/22.jpg)
22
Minh họa 2
Phần cấu hình config.<configuration>
<system.web>
<authentication mode="Forms">
<forms name="appNameAuth" loginUrl="login.aspx" >
</forms>
</authentication>
<compilation debug="true"/>
</system.web>
<!--MÔ TẢ CÁC TRANG CẦN BẢO VỆ-->
<location path="SecurePage.aspx">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
</configuration>
![Page 23: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/23.jpg)
23
Minh họa 2
Tạo trang index.aspx
Trang này chứa các link đến 2 trang
SecurePage.aspx
Public.aspx.
Index.aspx
![Page 24: NỘI DUNG - aspnhatnghe.weebly.com5 Authentication Việc sử dụng chức năng login rất thường phổ biến trong ứng dụng web Hạn chế user truy cập vào vùng](https://reader034.vdocuments.mx/reader034/viewer/2022052012/60295357d5aea205282071f7/html5/thumbnails/24.jpg)
24
Minh họa 2
Chạy thử nghiệm