asp.net2.0ロール管理、詳細手順
11194 ワード
step 1:
C# code
step 2:
ロール制御が必要なディレクトリの下でWebを新規作成します.config、以下の構成
C# code
step 3:
ログインコード、乗船券を取得
C# code
Step 4:(手動でロールを作成)
global.asax中
C# code
C# code
<
authentication mode
=
"
forms
"
>
<
forms name
=
"
.ASPXAUTH
"
loginUrl
=
"
/login.aspx
"
timeout
=
"
30
"
path
=
"
/
"
>
</
forms
>
</
authentication
>
step 2:
ロール制御が必要なディレクトリの下でWebを新規作成します.config、以下の構成
C# code
<
authorization
>
<
allow users
=
"
comma-separated list of users
"
roles
=
"
comma-separated list of roles
"
verbs
=
"
comma-separated list of verbs
"
/>
<
deny users
=
"
comma-separated list of users
"
roles
=
"
comma-separated list of roles
"
verbs
=
"
comma-separated list of verbs
"
/>
</
authorization
>
step 3:
ログインコード、乗船券を取得
C# code
FormsAuthenticationTicket Ticket
=
new
FormsAuthenticationTicket (
1
,user,DateTime.Now,
DateTime.Now.AddMinutes(
30
),
false
,userRoles,
"
/
"
) ;
//
string
HashTicket
=
FormsAuthentication.Encrypt (Ticket) ;
//
HttpCookie UserCookie
=
new
HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ;
//
Cookie
Context.Response.Cookies.Add (UserCookie) ;
//
Cookie
Step 4:(手動でロールを作成)
global.asax中
C# code
protected
void
Application_AuthorizeRequest(
object
sender, System.EventArgs e) { HttpApplication App
=
(HttpApplication) sender; HttpContext Ctx
=
App.Context ;
//
Http HttpContext
if
(Ctx.Request.IsAuthenticated
==
true
)
//
role
{ FormsIdentity Id
=
(FormsIdentity)Ctx.User.Identity ; FormsAuthenticationTicket Ticket
=
Id.Ticket ;
//
string
[] Roles
=
Ticket.UserData.Split (
'
,
'
) ;
//
role
Ctx.User
=
new
GenericPrincipal (Id, Roles) ;
//
Identity GenericPrincipal , role
} }