簡體   English   中英

如何設置ASP.NET登錄名以允許稍后在會話中檢索UserName或UserId?

[英]How can I set up ASP.NET login to allow the UserName or UserId to be retrieved later on in the session?

我正在嘗試為我的網站創建一個登錄系統,創建了一個自定義的login.ascx,當用戶單擊[Login]時,div會彈出login.ascx的內容。

然后,在用戶輸入其憑據后,他們單擊“登錄”按鈕。 他們通過登錄單擊功能中的以下代碼進行驗證並登錄:

if( Membership.ValidateUser( userName.Text, password.Text ) )
{
    //Here is where I'm not sure what to do   
}
else
{
    LoginError.Visible = true;
}

因此,在我不確定該怎么做的部分中,我希望用戶登錄(不確定這是否意味着創建身份驗證票)。 接下來是實際登錄用戶的下一步,我不希望他們被重定向到任何地方,因為他們已經在正確的頁面上了。

我還希望以后能夠檢索其用戶名或用戶ID,以便在我的Web服務中使用。 因此,為此,我應該執行Session.Add來創建新的會話值,或者是否存在其他一些首選的數據存儲方式?

為了驗證用戶,

    FormsAuthenatication.SetAuthCookie(username, false/*true if you want to remember the user's login*/);

這將使用戶登錄。以后可以使用

Page.User.Identity.Name

檢索當前用戶的用戶名並

Page.User.Identity.IsAuthenticated

檢查用戶是否已登錄。

無需將其存儲在Session 只需使用:

FormsAuthentication.SetAuthCookie

向客戶端發送身份驗證票證。 然后使用HttpContext.Current.User.Identity稍后進行檢索。

我發現使用會員資格提供者非常有用,我會推薦它

Scott Guthrie在此發布了很棒的博客

http://weblogs.asp.net/scottgu/archive/2006/05/07/ASP.NET-2.0-Membership-and-Roles-Tutorial-Series.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM