天天看點

關于Membership/Role您可能不知道的細節

1.關于System.Web.Security.Membership.ValidateUser("username", "password")

每次調用這個時,您注意到背後都執行了哪些sql語句嗎?

(a)使用者名不存在時,将執行以下語句

exec dbo.aspnet_CheckSchemaVersion @Feature=N'Common',@CompatibleSchemaVersion=N'1'

exec dbo.aspnet_CheckSchemaVersion @Feature=N'Membership',@CompatibleSchemaVersion=N'1'

exec dbo.aspnet_Membership_GetPasswordWithFormat @ApplicationName=N'APP_LUCKTY',@UserName=N'username',@UpdateLastLoginActivityDate=1,@CurrentTimeUtc=''2009-06-02 12:22:34:563''

(b)使用者名存在時,除上面的語句外,還将多執行以下二條語句

exec sp_reset_connection

exec dbo.aspnet_Membership_UpdateUserInfo @ApplicationName=N'APP_LUCKTY',@UserName=N'[email protected]',@IsPasswordCorrect=0,@UpdateLastLoginActivityDate=1,@MaxInvalidPasswordAttempts=999,@PasswordAttemptWindow=999,@CurrentTimeUtc=''2009-06-02 12:26:04:173'',@LastLoginDate=''2009-06-02 12:15:53:860'',@LastActivityDate=''2009-06-02 12:15:53:860''

關于sp_reset_connection,這個是幹啥用的?

sp_reset_connection 存儲過程由 SQL Server 用來支援事務中的遠端存儲過程調用。從連接配接池中重用連接配接時,該存儲過程還将導緻激發Audit Login 和 Audit Logout 事件。

2.if (User.Identity.IsAuthenticated){...}或if (User.IsInRole("RoleName")){...}時,系統是從哪裡知道目前使用者是否已經登入(或是否屬于某一角色)?

(a.猜測一:)會查詢資料庫嗎?當然不會,不信可以監測一下資料庫的sql語句

(b.猜測二:)是在Session裡嗎?也不是,不信EnableSessionState="False"再試下,好象還是能運作

(c.猜測三:)是在Cookie裡嗎?對了,不信把Cookie禁用掉(建議用最BT的辦法,把Documents and Settings\Administrator\Cookies目錄設定為任何使用者都無權讀取),然後就登入不了系統

3.除了FormsAuthentication.SignOut(),還有其它辦法登出“目前”使用者嗎,或者如何登出“指定”使用者? 

從2中很容易想到,隻要能找到特定使用者的用戶端Cookie,并使其過期就可以了,看下面的代碼:

HttpCookie _cookie = FormsAuthentication.GetAuthCookie(userName, true);

_cookie.Expires = DateTime.Now.AddDays(-1);

HttpContext.Current.Response.Cookies.Add(_cookie);

作者:菩提樹下的楊過