chapter 8 (security)

Post on 12-Apr-2017

364 Views

Category:

Education

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 8

ហោ សូហនឿន Web Form Security 1

Authorization និង Authentication

ហោ សូហនឿន Web Form Security 2

ASP.NET ផ្តល់ឲ្យអ្នកអ្ភិវឌ្ឍ Websites មានសមត្ថភាពទាំងការធ្វើ authentication និង authorization.

Authentication គឺជាការកាំណត់្អ្ត្តសញ្ញា ណរបស់ users។

Authorization គឺជាការផ្តល់សិទ្ធិដល់ users។

Authentication និង authorization ធដើរតួ្នាទី្យ៉ាងសាំខាន់កនុងការងារ security របស់ Website មួយធ្រោះការងាររបស់ពួកវាគឺការររការឈ្លា នរនពី្បភពអ្នកធ្បើដដលធយើងមិនចង់បាន។

Authentication កនុង ASP.NET

ហោ សូហនឿន Web Form Security 3

Name Description

FormsAuthentication Authentication is performed using a form, and the identity is

stored in a cookie or in the URL. This authentication type is

the most common.

WindowsAuthentication This option is used in intranets because it requires Windows credentials to be sent by the browser.

UrlAuthorization Authorization is performed using UrlAuthorizationModule, which checks web.config for authorized users and checks

roles against a specified set of URLs.

FileAuthorization Authorization is performed using the

FileAuthorizationModule, by checking the file ACLs of the

requested file.

Form Authentication

ហោ សូហនឿន Web Form Security 4

ធៅធពលធយើងធ្វើការជាមួយ Security ធៅកនុង website ធយើង្ត្ូវធ វ្ើយ៉ាងណាធដើមបីស្គា ល់អ្នកដដលកាំពុងធ្បើ្បាស់ Website (Authentication) ដូចជា username និង

password ដដលបាន Log ចូល។ FormsAuthentication គឺជាជធ្មើសមួយរបស់

ASP.NET ដដលផ្តល់ឲ្យធយើងនូវ methods មួយចាំនួនដដលធយើង្ត្ូវការធ្បើស្មាប់ ការងារ Authentication

របស់ Website មួយធៅធលើ្បព័នធ Internet។

Form Authentication Configuration

ហោ សូហនឿន Web Form Security 5

<authentication mode="Forms">

<forms name=".LoginCookie" loginUrl="Login.aspx"

timeout="50" cookieless="UseUri">

</forms>

</authentication>

Form Authentication

ហោ សូហនឿន Web Form Security 6

ធៅធពលធយើងធ្វើការជាមួយ Security ធៅកនុង website ធយើង្ត្ូវធ វ្ើយ៉ាងណាធដើមបីស្គា ល់អ្នកដដលកាំពុងធ្បើ្បាស់ Website (Authentication) ដូចជា username និង

password ដដលបាន Log ចូល។ FormsAuthentication គឺជាជធ្មើសមួយរបស់

ASP.NET ដដលផ្តល់ឲ្យធយើងនូវ methods មួយចាំនួនដដលធយើង្ត្ូវការធ្បើស្មាប់ ការងារ Authentication

របស់ Website មួយធៅធលើ្បព័នធ Internet។

Authorization

ហោ សូហនឿន Web Form Security 7

Authorization គឺជាដាំធណើ រការននការកាំណត់្សិទ្ធិឲ្យអ្នកធ្បើ។

បនាា ប់ Authorization ធ ើយ pages ទាំងឡាយធៅធលើ Website ដដល្តូ្វបានកាំណត់្ Security អាច្តូ្វបានធបើកដ៏រាបណា user បានបញ្ចូ ល username និង password ្តឹ្ម្តូ្វ។

ដូធចនោះរាល់ធពលដដលធគពាយមធបើក pages ដដល្ត្ូវបានកាំណត់្ Security ធពលធនាោះ Website នឹងធៅដាំធណើ រការ page “Login.aspx” ធោយសវ័យ្បវត្តិ។

Authorization

ហោ សូហនឿន Web Form Security 8

ជាទូ្ធៅ Authorization ្តូ្វបានធ វ្ើធៅកនុង “Web.Config” Page របស់ Website ដូចខាងធ្កាម។

Allow មានន័យថា “អ្នុញ្ញា ត្ ”។

Deny មានន័យថា “មិនអ្នុញ្ញា ត្ ”។

Users សាំធៅធលើ user របស់ Website។

Role សាំធៅធលើ role របស់ Website ។

<authorization>

<allow users="……" />

<deny roles="……" />

</authorization>

Authorization

ហោ សូហនឿន Web Form Security 9

User របស់ Website អាច្ត្ូវបានកាំណត់្ដូចខាងធ្កាម។ 1. * មានន័យថា “្គប់ user”

2. ? មានន័យថា “user ដដលបាន Login” (anonymous)។

3. Users សាំធៅធលើ user ជាក់លាក់ណាមួយ។

ឧទ រណ៏ <configuration> <location path=“home.aspx">

<system.web>

<authorization>

<allow users=“*" />

</authorization>

</system.web>

</location>

</configuration>

Credential User Accounts

ហោ សូហនឿន Web Form Security 10

ជា users ដដ្លូវបានរកាទុ្កធៅកនុង web.config file របស់ Website។

ខាងធ្កាមគឺជាការបធងកើត្ Credential user ចាំនួនបីរកាទុ្កធៅកនុង

web.config file របស់ Website។

<authentication mode="Forms"> <forms name=".LoginCookie" loginUrl="Login.aspx"

timeout= "30“ cookieless="UseUri"> <credentials passwordFormat="Clear"> <user name="Admin" password="123" /> <user name="sonoeun" password="123" /> <user name="numuser" password="123" /> </credentials> </forms> </authentication>

Login ធោយធ្បើ Credential User Account

ហោ សូហនឿន Web Form Security 11

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As

System.EventArgs) Handles Button1.Click

If (FormsAuthentication.Authenticate(txtUserName.Text, txtPassw.Text)) Then

FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False)

End If

End Sub

Membership API

ហោ សូហនឿន Web Form Security 12

The Membership API សាំណុាំ នន programming APIs

ដដល្តូ្វបានធ្បើ្បាស់ធដើមបី interact ជាមួយ

membership features កនុង ASP.NET application។ ASP.NET ផ្តល់នូវ SQL Database មួយជាធរសចធៅកនុង

ASP.NET Application ស្មាប់រកាទុ្ក user របស់ Membership API ធឈ្លម ោះ “App_Data”។

ការបធងកើត្ Membership API

ហោ សូហនឿន Web Form Security 13

Web Application Developer អាចបធងកើត្ login account

(user) របស់ membership API ធោយការធ្បើ ASP.Net

Configuration។

Website មួយអ្នុញ្ញា ត្ឲ្យ users បធងកើត្ login account

(user) របស់ membership API ធោយការធ្បើ CreateUserWizards Control។

ការបធងកើត្ Membership API ធោយធ្បើ ASP.NET Configuration

ហោ សូហនឿន Web Form Security 14

1. ចូលធៅ Website => ASP.Net Configuration

ASP.Net

Configuration

ការបធងកើត្ Membership API ធោយធ្បើ ASP.NET Configuration

ហោ សូហនឿន Web Form Security 15

2. ចូលធៅ Security

3. ចូលធៅ Create User

ការបធងកើត្ Membership API ធោយធ្បើ ASP.NET Configuration

ហោ សូហនឿន Web Form Security 16

កនុងករណី Security មិនមាន Create User tab ធយើង្ត្ូវកាំណត់្ Authentication របស់ Website ធោយកាំណត់្យក “From the Internet”

=> Select Authentication Type => From The Internet

Select Authentication Type

From the Internet

ការធ្បើ SQL Membership Provider

ហោ សូហនឿន Web Form Security 17

ធយើងអាចធ្បើ Database ផ្ទា ល់ខ្ាួនស្មាប់រកាទុ្ក Membership APIs បាន។

Web.Config អ្នុញ្ញា ត្ឲ្យធយើងធ វ្ើ configuration ធដើមបីរកាទុ្ក login accounts, roles, និង profile របស់ users ធៅកនុង Database ផ្ទា ល់ខ្ាួនបាន។

ធយើងកាំណត់្យក Database របស់ SQL មករកាទុ្ក login

accounts, roles, និង profile របស់ users។ Configuration ្ត្ូវបានអ្នុវត្តតាមលាំោប់លាំធោយដូចខាងធ្កាម៖

ការធ្វើ Web Configuration ស្មាប់ SQL Membership APIs

ហោ សូហនឿន Web Form Security 18

1. ដាំធណើ រការ “aspnet_regsql.exe” ធៅកនុង (C:\Windows\Microsoft.NET\Framework\v2.0\)។

Server Name

Database Name

ការធ្វើ Web Configuration ស្មាប់ SQL Membership APIs

ហោ សូហនឿន Web Form Security 19

2. ធ្វើ configuration ធៅកនុង Web.config ដូចខាងធ្កាម៖

<connectionStrings>

<add name="LocalCnnServer"

connectionString="server=MACBOOK-PC\SQLEXPRESS;

Database=dbHotel; Integrated Security=True"

providerName="System.Data.SqlClient"/>

<remove name="LocalSqlServer"/>

<add name="LocalSqlServer"

connectionString="server=MACBOOK-PC\SQLEXPRESS;

Database=dbHotel; Integrated Security=True"

providerName="System.Data.SqlClient"/>

</connectionStrings>

Role Manager

ហោ សូហនឿន Web Form Security 20

ចាំបាច់ណាស់ដដលធយើង្ត្ូវធរៀបចាំ users របស់ Website ធៅតាម្កុមនីមួយ ធដើមបីងាយរសួលស្មាប់ការារកាំណត់្សិទ្ធិ និង្គប់្គង users។

Role Manager គឺជាដផ្នកមួយរបស់ WSAT (Webs Site

Administration Tools) ដដលអ្នុញ្ញា ត្ឲ្យ Web

Developers ធរៀបចាំ users ោក់ចូលតាម Role នីមួយៗបាន។

ការបធងកើត្ Roles

ហោ សូហនឿន Web Form Security 21

ធៅកនុង Security=>Select ធលើ Enable Roles=>Select ធលើ Create or Manage Roles

ការធរៀបចាំ Users តាម Roles នីមួយៗ

ហោ សូហនឿន Web Form Security 22

ចូលធៅ Manage Users

ចុច Edit roles ធដើមបី Add user ចូលកនុង Role

Login Control

ហោ សូហនឿន Web Form Security 23

Login Control មានភាា ប់មកជាមួយនូវ Username TextBox

Password TextBox

Error Message (Validation)

Remember for login next time

និង Log In Button

Login control ធ្បើ្បាស់ authentication ដដលបានកាំណត់្ធៅកនុង “web.config” (FormAuthentication)។

Login Control

ហោ សូហនឿន Web Form Security 24

ធយើងអាចបតូរទ្ាំរង់របស់ Login Control ធោយចូលធៅ Auto Format

Auto Format

Login Control ជាមួយ Credential User Account

ហោ សូហនឿន Web Form Security 25

Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As

System.Web.UI.WebControls.AuthenticateEventArgs) Handles

Login1.Authenticate If (FormsAuthentication.Authenticate(Login1.UserName, Login1.Password)) Then

FormsAuthentication.RedirectFromLoginPage(Login1.UserName, False)

End If End Sub

LoginName និង LoginStatus Controls

ហោ សូហនឿន Web Form Security 26

LoginName Control ជា Web Security Control មួយដដលមាននាទី្ទញយក និង បងាា ញធឈ្លម ោះរបស់ user ដដលបាន Log ចូល។

LoginStatus Control ជា Web Security Control មួយដដលអ្នុញ្ញា ត្ឲ្យ Web Developer បធងកើត្ Log Out control ស្មាប់ឲ្យ user ធ វ្ើការ Log

Out វញី។

LoginName

LoginStatus

Displayed

CreateUserWizard Control

ហោ សូហនឿន Web Form Security 27

ដូចបានធលើកធ ើងធៅកនុង Membership API ធដើមបីអ្នុញ្ញា ត្ឲ្យ user

អាចចុោះធឈ្លម ោះជាសមាជិកធៅកនុង Website របស់ធយើងបាន ធយើង្ត្ូវមាន Page ស្មាប់បធងកើត្ user មួយដដលមាន CreateUserWizard

control។

<asp:CreateUserWizard ID="CreateUserWizard1"

runat="server">

<WizardSteps>

<asp:CreateUserWizardStep runat="server" />

<asp:CompleteWizardStep runat="server" />

</WizardSteps>

</asp:CreateUserWizard>

CreateUserWizard Control

ហោ សូហនឿន Web Form Security 28

ChangePassword Control

ហោ សូហនឿន Web Form Security 29

ChangePassword ជា Web Security Control មួយដដល្ត្ូវបានធ្បើធដើមបីឲ្យ user អាចបតូរ Password របស់ធគបាន។

ធដើមបីធ្វើការងារធនោះធយើង្តូ្វមាន Page ស្មាប់ recover

password មួយដដលមាន ChangePassword control។

<form id="form1" runat="server">

<div>

<asp:ChangePassword

ID="ChangePassword1"

runat="server">

</asp:ChangePassword>

</div>

</form>

RecoveryPassword Control

ហោ សូហនឿន Web Form Security 30

RecoveryPassword អ្នុញ្ញា ត្ឲ្យ user អាចរបបានមកវញិនូវ Password ្បសិនធបើធគធភាច Password.

ធដើមបីធ្វើការងារធនោះធយើង្ត្ូវមាន Page ស្មាប់ recover

password មួយដដលមាន RecoverPassword control។

<form id="form1" runat="server">

<div>

<asp:PasswordRecovery

ID="PasswordRecovery1"

runat="server">

</asp:PasswordRecovery>

</div>

</form>

Chapter 8

ហោ សូហនឿន Web Form Security 31

top related