[英]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在此发布了很棒的博客
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.