[英]ASP.NET Membership: how to set the user as logged in
我正在努力讓會員提供商工作。
到目前為止,我有:
<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
</asp:Login>
打電話:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if(Membership.ValidateUser(Login1.UserName, Login1.Password))
{
Response.Redirect("/admin/default.aspx");
// Set the user as logged in?
}
}
如果我輸入正確的登錄名/密碼,ValidateUser函數將返回true。 所以我的問題是:如何將用戶設置為登錄?
我在我的網頁上測試這個:
protected void Page_Load(object sender, EventArgs e)
{
if ( Membership.GetUser()==null)
{
Response.Redirect("/admin/login.aspx");
}
// else "you are logged in, congratulations"
}
我會使用默認功能,但它只是不起作用,谷歌搜索讓我覺得我會通過實際重新編碼所有自己來節省時間。
一切都會有所幫助!
編輯 :關於接受的答案,它是“如何設置用戶登錄”是正確的,並且工作正常。 它沒有解決我的具體問題,只是其中的一部分。 如果你認為評論你會發現有趣的指針。
編輯2和解決方案:好的,由於所有的評論我終於解決了。 這是我做的,它比我預期的更簡單:
檢查登錄狀態的頁面:
protected void Page_Load(object sender, EventArgs e)
{
if ( !Request.IsAuthenticated)
{
Response.Redirect("/admin/login.aspx");
}
登出:
protected void LoginStatus1_Logout(object sender, LoginCancelEventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("/admin/login.aspx");
}
}
web.config中:
<authentication mode="Forms" />
登錄:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if(Membership.ValidateUser(Login1.UserName, Login1.Password))
{
FormsAuthentication.SetAuthCookie(Login1.UserName, true);
Response.Redirect("/admin/default.aspx");
}
}
在調用Response.Redirect("/admin/default.aspx");
之前將其放入Login1_Authenticate
Response.Redirect("/admin/default.aspx");
FormsAuthentication.SetAuthCookie("username", true);
嘗試將代碼和Gromer的建議移至LoggedIn事件。
protected void Login1_LoggedIn(object sender, EventArgs e)
{
if(Membership.ValidateUser(Login1.UserName, Login1.Password))
{
FormsAuthentication.SetAuthCookie(Login1.UserName, true);
Response.Redirect("/admin/default.aspx");
}
}
編輯:像Gromer說的那樣,只有在用戶登錄后和重定向s / he之前必須執行某些業務代碼時才這樣做。
編輯編輯:Visual Studio將Authenticate事件描述為“調用以驗證用戶”,這意味着在調用事件之前未對用戶進行身份驗證。 因此,您無法確認用戶是否已登錄,因為他/她尚未經過身份驗證。
Gromer有答案,但您也可以查看這篇MSDN文章以了解更多信息:
雖然我不知道這會有多大幫助,但這是我用來辨別管理員用戶或普通用戶的樣板代碼。 對我來說很棒。
在您的登錄頁面上,可能是onclick創建您的用戶對象並使用此代碼調用某個函數(UserRole是一個包含您角色的枚舉):
If admin Then
If role = UserRole.Admin Then
RedirectFromLoginPage(username & "|" & userid, False)
Return True
Else
Return False
End If
Else
If String.IsNullOrEmpty(Current.Request.QueryString("ReturnUrl")) Then
SetAuthCookie(username & "|" & userid, True)
Else
RedirectFromLoginPage(username & "|" & userid, True)
End If
Return True
End If
在你的web.config中:
<location path="admin">
<system.web>
<authorization>
<allow roles="Admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
.....
<system.web>
<authentication mode="Forms">
<forms loginUrl="/registration/login.aspx" timeout="129600"/>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
...如果你真的想要,請在你的Global.asax頁面中:
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
If Request.IsAuthenticated Then
''
'get your roles for the current user'
''
Dim userRoles() As String = Split(roles, "|")
'Add the roles to the User Principal'
HttpContext.Current.User = New GenericPrincipal(User.Identity, userRoles)
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.