Thitk & Lp trình Web 2
Bài 9
BoBomtmt ngng dngdng Web ASP.NETWeb ASP.NET
Ths. TrnTh Bích Hnh
Khoa CNTT Khoa CNTT –– H.KHTNH.KHTN
© 2009 Khoa Công ngh thông tin
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
NiNi
dungdung
NiNi
dungdung
Mts khái nimv Bomt
Các cp đ bomt trong mt ng dng ASP.NET
ng nhp, Qun lý thành viên, phân quyn trong
A
SP.NET
© 2009 Khoa CNTT - HKHTN
2
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
NiNi
dungdung
NiNi
dungdung
Mts khái nimv Bomt
Các cp đ bomt trong mt ng dng ASP.NET
ng nhp, Qun lý thành viên, phân quyn trong
A
SP.NET
© 2009 Khoa CNTT - HKHTN
3
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
MtMt
ss
KháiKhái
nimnim
BoBo
mtmt
MtMt
ss
KháiKhái
nimnim
BoBo
mtmt
Authentication – Chng thc
–
Qui trình chng thcngi dùng
– Thng yêu cungi dùng nhpTênđng nhp& mt
khu
khu
Ath i ti
C
A
u
th
or
i
za
ti
on –
C
p quy
n
– Qui trình quyt đnh user đãchng thc đc phép truy cp
các
tài
nguyên
nht
đnh
các
tài
nguyên
nht
đnh
– Thng cp quyndatrênLoingi dùng (role-based
authorization)
© 2009 Khoa CNTT - HKHTN
4
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
NiNi
dungdung
NiNi
dungdung
Mts khái nimv Bomt
Các cp đ bomt trong mt ng dng ASP.NET
ng nhp, Qun lý thành viên, phân quyn trong
A
SP.NET
© 2009 Khoa CNTT - HKHTN
5
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CácCác
cpcp
đđ
bobo
mtmt
trongtrong
ASP NETASP NET
CácCác
cpcp
đđ
bobo
mtmt
trongtrong
ASP
.
NETASP
.
NET
IIS
ASP.NET
© 2009 Khoa CNTT - HKHTN
6
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
IIS SecurityIIS Security
IIS
SecurityIIS
Security
Là cp đ bomt đutiênđcthchin khi có yêu cu
đn
webpage (request)
đn
webpage
(request)
.
Các bckimtra:
IIS
ki
t
đ
h
IP
t
ó
đ
t
à
–
IIS
ki
m
t
ra
đ
ac
h
IP
c
a reques
t
c
ó
đ
c
t
ruy c
pv
à
o
domain hay không
–
C
h
n
g
t
h
c
n
g
i
dù
n
g
(
n
u
c
n
)
C g
t c
g
dù g
(
u
c
)
– Nu thành công chuyn request qua ASP.NET
–
N
g
cl
i thôn
g
báo cho n
g
i dùn
g
bit khôn
g
đ
c
p
hé
p
g
g
g
g
g
pp
truy cp
© 2009 Khoa CNTT - HKHTN
7
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
IIS SecurityIIS Security
IIS
SecurityIIS
Security
© 2009 Khoa CNTT - HKHTN
8
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CácCác
hìnhhình
thcthc
chngchng
thcthc
trongtrong
IISIIS
CácCác
hìnhhình
thcthc
chngchng
thcthc
trongtrong
IISIIS
Anonymous
Basic
Digest
Integrated Windows Authentication
Certificate
© 2009 Khoa CNTT - HKHTN
9
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
IIS Anonymous AuthenticationIIS Anonymous Authentication
IIS
Anonymous
AuthenticationIIS
Anonymous
Authentication
Mc đnh IIS cho phép anonymous truy cpvàomt ng
dng
Web
dng
Web
© 2009 Khoa CNTT - HKHTN
10
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
IIS Basic & Digest AuthenticationIIS Basic & Digest Authentication
IIS
Basic
&
Digest
AuthenticationIIS
Basic
&
Digest
Authentication
Basic
Yê
h
&d
–
Yê
uc
uuser n
h
p username
&
passwor
d
– Username & password đc mã hóa và truyn qua HTTP header
–
Username & password
đc
kim
tra
khp
vi
tài
khon
Windows
Username
&
password
đc
kim
tra
khp
vi
tài
khon
Windows
trên server
– Ch nên s dng viHTTPS
Digest
Password
đc
x
lý
vi
hàm
hash
và
gi
lên
server
–
Password
đc
x
lý
vi
hàm
hash
và
gi
lên
server
– Server thchin cùng x lý vi hàm hash vi password trên server
và kimtravigiátr nhn đc
© 2009 Khoa CNTT - HKHTN
11
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
IIS Integrated Windows AuthenticationIIS Integrated Windows Authentication
IIS
Integrated
Windows
AuthenticationIIS
Integrated
Windows
Authentication
IIS chng thc user vi tài khon user trên Windows
Khi s dng trong mng intranet, Windows Authentication
cho phép IIS quyt đnh yêu cucangi dùng davào
vic
đng
nhp
ca
user
trên
Windows
vic
đng
nhp
ca
user
trên
Windows
© 2009 Khoa CNTT - HKHTN
12
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
IIS Certificate AuthenticationIIS Certificate Authentication
IIS
Certificate
AuthenticationIIS
Certificate
Authentication
Certificate
–
S dng các certificate causer đ
thchinvicchng
thc
–
Trong
mã
hóa
khóa
công
khai
(public
-
key cryptography)
mt
–
Trong
mã
hóa
khóa
công
khai
(public
-
key
cryptography)
mt
certificate s dng mtch ký đint (digital signature) và
mt khóa công khai đikèmvi đnh danh ngi dùng
© 2009 Khoa CNTT - HKHTN
13
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
ASP NET SecurityASP NET Security
ASP
.
NET
SecurityASP
.
NET
Security
© 2009 Khoa CNTT - HKHTN
14
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
ImpersonationImpersonation
ImpersonationImpersonation
Mc đnh Impersonation là disabled
–
T
tc ASP.NET request đcthcthibi đnh danh mc
đnh dành cho cho các ng dng ASP.NET
•
Tài
khon
ASPNET
t
đng
đc
to
khi
cài
.NET Framework
Tài
khon
ASPNET
t
đng
đc
to
khi
cài
.NET
Framework
• Tài khon NETWORK SERVICE đc đnh nghasn trong
Windows Server 2003 và có quyntruycpnh tài khon
ASPNET
ASPNET
© 2009 Khoa CNTT - HKHTN
15
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
ImpersonationImpersonation
ImpersonationImpersonation
Khi thitlp impersonation là enabled cho mt ng dng
Web
ng
dng
s
đc
thc
thi
di
đnh
danh
đc
ch
Web
,
ng
dng
s
đc
thc
thi
di
đnh
danh
đc
ch
đnh bimt security token truynt IIS
S
dng
impersonation
khi
S
dng
impersonation
khi
– ng dng web davàoIIS đ chng thcuser
–
Server cho
p
hé
p
host nhiu n
g
dn
g
t nhiu khách hàn
g
khác
pp
g
g
g
nhau, cung cpchomi ng dng web mttàikhon Windows
riêng bit đ ngn không cho ng dng này truy cp vào tài nguyên
ca ng dng khác
// Web.Config
<authentication mode="Windows" />
© 2009 Khoa CNTT - HKHTN
16
<identity impersonate="true" />
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Code Access SecurityCode Access Security
Code
Access
SecurityCode
Access
Security
Tính nng đch tr trong CLR
Qi
đ h
h
à
b
h
hé
á
l i
ã
l h
t
bl
Q
u
i
đ
n
h
n
h
ng r
à
ng
b
u
cc
h
op
hé
pc
á
c
l
o
i
m
ã
l
n
h
t
rong assem
bl
y
đc quynthcthi
–
Trust level là m
tt
p
các lu
t đnh n
g
ha các class tron
g
p
g
g
.NET Framework mà ng dng ASP.NET đc phép s dng
// Web.config
<system.web>
<trust level=
"
Medium
"
/>
<trust
level= Medium />
</system.web>
© 2009 Khoa CNTT - HKHTN
17
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Trust LevelsTrust Levels
Trust
LevelsTrust
Levels
Full
Tt
NET l
đ
hé
d
&
th
thi
–
Tt
c
.
NET
c
l
ass
đ
cp
hé
ps
d
ng
&
th
c
thi
High
–
Không
đc
s
dng
unmanaged code enterprise services
Không
đc
s
dng
unmanaged
code
,
enterprise
services
,
reflection
Medium
– ng dng chđc phép truy xut trong cutrúcth mccanó
Low
Rd
lliti
–
R
ea
d
-on
l
y app
li
ca
ti
on
Minimal
–
Không
đc
phép
truy
cp
tài
nguyên
© 2009 Khoa CNTT - HKHTN
Không
đc
phép
truy
cp
tài
nguyên
18
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
ASP NET Authentication
ASP
.
NET
Authentication
None
ASP NET
khô
th
hi
i
–
ASP
.
NET
khô
ng
th
c
hi
nv
i
c
chng thc
Windows
– S dng ktqu t c ch
chng thc trong cuhìnhca
IIS
Forms
– Cho phép chng thc thông
qua
f
o
rm
đ
n
g
nh
p
qua o
đ g
p
Passport
– S dng dch v chng thc
Mi ft P t
© 2009 Khoa CNTT - HKHTN
c
a
Mi
croso
ft
P
asspor
t
19
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Forms AuthenticationForms Authentication
Forms
AuthenticationForms
Authentication
© 2009 Khoa CNTT - HKHTN
20
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Forms AuthenticationForms Authentication
Forms
AuthenticationForms
Authentication
© 2009 Khoa CNTT - HKHTN
21
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Forms AuthenticationForms Authentication
Forms
AuthenticationForms
Authentication
Thuc tính Ý ngha
Cookieless
Qui
đnh
vic
chng
thc
lu
trong
cookie:
Cookieless
(UseDefaultProfile)
Qui
đnh
vic
chng
thc
lu
trong
cookie:
UseCookies, UseUri (nhúng ticket trong URL), và
AutoDetect
defaultUrl
n
g
dn đntran
g
web sau khi chn
g
th
cuser
g
g
g
thành công
loginUrl ng dn đn trang login
protection (All)
Qui
đnh
cách
thc
mã
hóa
authentication ticket:
protection
(All)
Qui
đnh
cách
thc
mã
hóa
authentication
ticket:
All (hash & encrypte), Encryption, None, Validation
(check ticket có b sa đi)
re
q
uireSSL
(
false
)
Yêu cus dn
g
ktni SSL khi tru
y
nd liu
q
()
g
y
© 2009 Khoa CNTT - HKHTN
22
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Forms AuthenticationForms Authentication
Forms
AuthenticationForms
Authentication
Thuc tính Ý ngha
domain
Domain
chng
thc
cookie
domain
Domain
chng
thc
cookie
enableCrossAppRedirects
(false)
Cho phép nhiu ng dng web s dng cùng 1
chng thc
name
Tên
cookie
name
Tên
cookie
path ng dn cho cookie
slidingExpiration (false) Reset lithi gian expire ca cookie sau lntruy
i
c
pcu
i
timeout (30 phút) Qui đnh thi gian expire ca cookie
// Web.Config
<authentication mode="Forms" >
<forms loginUrl="Login.aspx" timeout="86400"
slidingExpiration="true“ />
© 2009 Khoa CNTT - HKHTN
23
</authentication>
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
NiNi
dungdung
NiNi
dungdung
Mts khái nimv Bomt
Các cp đ bomt trong mt ng dng ASP.NET
ng nhp, Qun lý thành viên, phân quyn trong
A
SP.NET
© 2009 Khoa CNTT - HKHTN
24
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Membership ProviderMembership Provider
Membership
ProviderMembership
Provider
Là tpcáclpca .NET cho phép developer x lý các
chc
nng
qun
lý
chng
thc
ngi
dùng
chc
nng
qun
lý
chng
thc
ngi
dùng
– Thêm ngi dùng mi
–
Lu
thông
tin
ngi
dùng
trong
CSDL
Lu
thông
tin
ngi
dùng
trong
CSDL
– Chng thcngi dùng
– Phân quynngi dùng
– Qunlýmtkhu(to, sa, reset)
Membership Provider mc đnh là SqlMembershipProvider
– ToCSDL aspnetdb trong SQLExpress và lu trong th mc
App_Data
© 2009 Khoa CNTT - HKHTN
25
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Role Management ProviderRole Management Provider
Role
Management
ProviderRole
Management
Provider
Là tpcáclpca .NET cho phép developer x lý các
chc
nng
qun
lý
cp
quyn
ngi
dùng
chc
nng
qun
lý
cp
quyn
ngi
dùng
– Thêm role mi
–
Ch
đnh
ngi
dùng
vào
các
roles
Ch
đnh
ngi
dùng
vào
các
roles
– Phân quyn cho phép ngi dùng truy cp vào tài nguyên web
Role Mana
g
ement Provider m
c đ
nh là S
q
lRoleProvide
r
g
q
© 2009 Khoa CNTT - HKHTN
26
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
ASP NET Login ControlsASP NET Login Controls
ASP
.
NET
Login
ControlsASP
.
NET
Login
Controls
CreateUserWizard
Login
LoginName & LoginStatus
LoginView
Chan
g
ePassword
g
PasswordRecovery
© 2009 Khoa CNTT - HKHTN
27
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CreateUserWizardCreateUserWizard
ControlControl
CreateUserWizardCreateUserWizard
ControlControl
© 2009 Khoa CNTT - HKHTN
28
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Login ControlLogin Control
Login
ControlLogin
Control
© 2009 Khoa CNTT - HKHTN
29
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
LoginNameLoginName
andand
LoginStatusLoginStatus
ControlsControls
LoginNameLoginName
and
and
LoginStatusLoginStatus
ControlsControls
© 2009 Khoa CNTT - HKHTN
30
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
LoginViewLoginView
ControlControl
LoginViewLoginView
ControlControl
© 2009 Khoa CNTT - HKHTN
31
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
ChangePasswordChangePassword
ControlControl
ChangePasswordChangePassword
ControlControl
© 2009 Khoa CNTT - HKHTN
32
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
PasswordRecoveryPasswordRecovery
ControlControl
PasswordRecoveryPasswordRecovery
ControlControl
© 2009 Khoa CNTT - HKHTN
33
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CC
uu
hìhì
nhnh
Mail Server (SMTP)Mail Server (SMTP)
CC
uu
hìhì
nhnh
Mail
Server
(SMTP)Mail
Server
(SMTP)
© 2009 Khoa CNTT - HKHTN
34
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CC
uu
hìhì
nhnh
Mail Server (SMTP)Mail Server (SMTP)
CC
uu
hìhì
nhnh
Mail
Server
(SMTP)Mail
Server
(SMTP)
// Web.Config
<configuration>
<configuration>
<system.net>
<mailSettings>
i
i@ i
<smtp
d
el
i
veryMethod="Network" from=“yourma
i
l
@
gma
i
l.com">
<network
host="
smtp.gmail.com" port="465"
“
il@ il
"d“
d
"
userName=
“
yourma
il@
gma
il
.com
"
passwor
d
=
“
y
our passwor
d
"
defaultCredentials="true"/>
</smtp>
/
ilS i
<
/
ma
ilS
ett
i
ngs>
</system.net>
…
© 2009 Khoa CNTT - HKHTN
</configuration>
35
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
XX
lyly
GG
ii
mailmail
ll
yy
ll
ii
PasswordPassword
XX
lyly
GG
ii
mail
mail
ll
yy
ll
ii
PasswordPassword
using System.Net;
using System.Net.Mail;
protected void PasswordRecovery1_SendingMail(object sender,
MailMessageEventArgs e) {
try {
SmtpClient smtpSender = new SmtpClient(“smtp server”, “smtp port”);
smtpSender.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpSender.Credentials = new NetworkCredential(“
username”, “password”);
smtpSender.EnableSsl = true;
smtpSender.Send(e.Message);
} catch (Exception ex) {
RWit
("Th bl di th il " + )
R
esponse.
W
r
it
e
("Th
ere was a pro
bl
em sen
di
ng
th
e ema
il
.
"
+
ex
)
;
}
e.Cancel = true;
}
© 2009 Khoa CNTT - HKHTN
}
36
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
LL
yy
thôngthông
tintin
cc
uu
hìhì
nhnh
Mail ServerMail Server
trongtrong
Web configWeb config
LL
yy
thôngthông
tin
tin
cc
uu
hìhì
nhnh
Mail
Server
Mail
Server
trongtrong
Web
.
configWeb
.
config
using System.Web.Configuration;
using
System Net Configuration
;
using
System
.
Net
.
Configuration
;
Configuration config =
WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings =
(MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
// settings.Smtp.Network.Host
// settings.Smtp.Network.Port
// settings.Smtp.Network.Username
//
s
ettings.Smtp.Network.Passwor
d
© 2009 Khoa CNTT - HKHTN
37
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
XX
lyly
ll
ii
GG
ii
mailmail
XX
lyly
ll
ii
GG
ii
mailmail
protected void PasswordRecovery1_SendMailError(object sender,
SendMailErrorEventArgs
e)
SendMailErrorEventArgs
e)
{
e.Handled = true;
© 2009 Khoa CNTT - HKHTN
PasswordRecovery1.SuccessText = e.Exception.Message;
}
38
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CuCu
hìnhhình
ss
dngdng
DatabaseDatabase
riêngriêng
chocho
Membership & RoleMembership & Role
CuCu
hìnhhình
ss
dngdng
Database
Database
riêngriêng
chocho
Membership
&
RoleMembership
&
Role
Bc1: ThcthiASP.NET Sql Server Registration Tool
đ
to
CSDL
đ
to
CSDL
– WINDOWS\Microsoft.NET\Framework\2.0.xxxx\
as
p
net re
g
s
q
l.exe
p
_
gq
Bc
2:
Cu
hình
ConnectionString
Membership
Bc
2:
Cu
hình
ConnectionString
,
Membership
Provider & Role Provider trong Web.Config
Bc3: S dng Web Site Administration Tool đ cu
hình
p
hân
q
u
y
n
© 2009 Khoa CNTT - HKHTN
p
qy
– Menu > Website > ASP.NET Configuration
39
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CuCu
hìnhhình
ss
dngdng
DatabaseDatabase
riêngriêng
chocho
Membership & RoleMembership & Role
CuCu
hìnhhình
ss
dngdng
Database
Database
riêngriêng
chocho
Membership
&
RoleMembership
&
Role
Bc1
http://www codedigest com/FAQ/
16
-
How
-
to
-
Configure
-
ASP
-
Net
-
Membership
-
© 2009 Khoa CNTT - HKHTN
http://www
.
codedigest
.
com/FAQ/
16
How
to
Configure
ASP
Net
Membership
Providers-to-Use-Our-Own-Database aspx
40
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CuCu
hìnhhình
ss
dngdng
DatabaseDatabase
riêngriêng
chocho
Membership & RoleMembership & Role
CuCu
hìnhhình
ss
dngdng
Database
Database
riêngriêng
chocho
Membership
&
RoleMembership
&
Role
Bc2
<!
S t th ti t i f SQL S
>
<!
S
e
t
th
e connec
ti
on s
t
r
i
ng
f
or
SQL
S
erver
>
<connectionStrings>
<clear />
<add name="
SqlConn"
connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Test;"
/>
</connectionStrings
>
© 2009 Khoa CNTT - HKHTN
/>41
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CuCu
hìnhhình
ss
dngdng
DatabaseDatabase
riêngriêng
chocho
Membership & RoleMembership & Role
CuCu
hìnhhình
ss
dngdng
Database
Database
riêngriêng
chocho
Membership
&
RoleMembership
&
Role
Bc 2 (tt)
<t b>
<
sys
t
em.we
b>
<! Configure the Sql Membership Provider >
<membership defaultProvider=“MySqlMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add
name="
MySqlMembershipProvider"
type=
"
System Web Security SqlMembershipProvider
"
type=
System
.
Web
.
Security
.
SqlMembershipProvider
connectionStringName="SqlConn"
applicationName="/"
enablePasswordRetrieval="false"
enablePasswordReset="false"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat
="
Hashed
"
/
>
© 2009 Khoa CNTT - HKHTN
passwordFormat
Hashed
/
</providers>
</membership>
</system.web>
42
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CuCu
hìnhhình
ss
dngdng
DatabaseDatabase
riêngriêng
chocho
Membership & RoleMembership & Role
CuCu
hìnhhình
ss
dngdng
Database
Database
riêngriêng
chocho
Membership
&
RoleMembership
&
Role
Bc 2 (tt)
<t b>
<
sys
t
em.we
b>
<! Configure the Sql Role Provider >
<roleManager enabled ="true"
defaultProvider =“
MySqlRoleProvider" >
<providers>
<clear />
<add name =“
MySqlRoleProvider"
type=
"
System Web Security SqlRoleProvider
"
type=
System
.
Web
.
Security
.
SqlRoleProvider
connectionStringName="SqlConn"
applicationName="/"/>
</providers>
</roleManager>
</system.web>
© 2009 Khoa CNTT - HKHTN
43
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
CuCu
hìnhhình
ss
dngdng
DatabaseDatabase
riêngriêng
chocho
Membership & RoleMembership & Role
CuCu
hìnhhình
ss
dngdng
Database
Database
riêngriêng
chocho
Membership
&
RoleMembership
&
Role
Bc 3: Website Administration Tool
© 2009 Khoa CNTT - HKHTN
44
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Membership APIMembership API
Membership
APIMembership
API
Membership.Method
CreateUser
CreateUser
DeleteUser
FindUsersByEmail
protected void btnLogin_Click(object s, EventArgs e)
{
strin
g
usr = txtUser.Text;
FindUsersByName
GeneratePassword
GetAllUsers
g
string pwd = txtPassword.Text;
// Use the membership system to authenticate user
if ( Membership.ValidateUser(usr, pwd) )
FormsAuthentication RedirectFromLoginPage
(
usr true
);
GetAllUsers
GetNumberOfUsersOnline
GetUser
GtU N BE il
FormsAuthentication
.
RedirectFromLoginPage
(
usr
,
true
);
else
labError.Text = "User not found, try again";
}
G
e
tU
ser
N
ame
B
y
E
ma
il
UpdateUser
ValidateUser
© 2009 Khoa CNTT - HKHTN
45
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
MembershipUserMembershipUser
APIAPI
MembershipUserMembershipUser
APIAPI
MembershipUser.Method MembershipUser.Property
ChangePassword
Comment
ChangePassword
Comment
ChangePasswordQuestionAndAnswer CreationDate
GetPassword Email
ResetPassword IsApproved
UnLockUser IsLockedOut
IsOnline
LastActivityDate
LastLockoutDate
LastLoginDate
LastLoginDate
LastPasswordChangedDate
PasswordQuestion
© 2009 Khoa CNTT - HKHTN
UserName
46
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Ví
d
-
Qun
lý
danh
sách
User
trong
GridView
Ví
d
-
Qun
lý
danh
sách
User
trong
GridView
<asp:ObjectDataSource ID="odsUsers"
runat="server"
<asp:GridView ID="grdUsers“ runat="server"
DataSourceID="odsUsers"
TypeName="System.Web.Security.Membership"
SelectMethod="GetAllUsers"
DeleteMethod="DeleteUser">
<DeleteParameters>
AutoGenerateColumns="false"
AutoGenerateDeleteButton="true"
AutoGenerateSelectButton="true"
CellPadding
=
"
5
"
<asp:ControlParameter
ControlID="grdUsers" Type="string"
PropertyName="SelectedDataKey.Values[0]"
CellPadding
=
5
DataKeyNames=“UserName”>
<Columns>
<asp:BoundField HeaderText="Name"
DtFild
"
UN
"/>
Name="username" />
</DeleteParameters>
</asp:ObjectDataSource>
D
a
t
a
Fi
e
ld
=
"
U
ser
N
ame
"
/>
<asp:BoundField HeaderText="Email"
DataField="Email"/>
<asp:BoundField HeaderText="Create
Date
“
DataField
="
CreationDate
"/
>
Date
DataField
CreationDate
/
<asp:BoundField HeaderText="Last
Login“ DataField="LastLoginDate" />
</Columns>
</
asp:GridView
>
© 2009 Khoa CNTT - HKHTN
</
asp:GridView
>
47
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
Roles APIRoles API
Roles
APIRoles
API
Roles.Method Roles.Method
AddUsersToRole
GetAllRoles
AddUsersToRole
GetAllRoles
AddUsersToRoles GetRolesForUser
AddUserToRole GetUsersInRole
AddUserToRoles RemoveUserFromRole
CreateRole RemoveUserFromRoles
DeleteCookie RemoveUsersFromRole
DeleteCookie RemoveUsersFromRoles
FindUsersInRole RoleExists
IsUserInRole
IsUserInRole
© 2009 Khoa CNTT - HKHTN
48
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
VíVí
dd
HinHin
thth
danhdanh
sáchsách
RolesRoles
trongtrong
CheckBoxListCheckBoxList
VíVí
dd
HinHin
thth
danhdanh
sáchsách
Roles
Roles
trongtrong
CheckBoxListCheckBoxList
<asp:ObjectDataSource ID="odsRoles" runat="server"
TypeName
=
"
System Web Security Roles
"
TypeName
=
System
.
Web
.
Security
.
Roles
SelectMethod="GetAllRoles" />
Select user roles: <br/>
<asp:CheckBoxList ID="chkRoles" runat="server"
DataSourceID="
odsRoles" />
© 2009 Khoa CNTT - HKHTN
49
Thitk & Lp trình Web 2 – Bài 9: Bomt ng dng Web ASP.NET
TngTng
ktkt
TngTng
ktkt
IIS Security
ASP.NET Security
Membership & Role
© 2009 Khoa CNTT - HKHTN
50