如何设置ASP.NET登录以允许稍后在会话中检索UserName或UserId?

时间:2021-11-13 03:38:36

I'm trying to create a login system for my website, I've created a custom login.ascx and when the user clicks [ Login ] a div pops up with the contents of login.ascx.

我正在尝试为我的网站创建一个登录系统,我创建了一个自定义的login.ascx,当用户点击[Login]时,会弹出一个div,其中包含login.ascx的内容。

Then after the user enters their credentials, they click on the Login button. They get validated and logged in using this code in the login click function:

然后,在用户输入凭据后,他们会单击“登录”按钮。它们在登录点击功能中使用此代码进行验证并登录:

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

So in the section where I'm not sure what to do, I would like the user to get logged in (Not sure if that means creating an authentication ticket or whatnot). What does is the next step to actually log the user in, I don't want them to get redirected anywhere since they are on the correct page already.

所以在我不知道该怎么做的部分,我希望用户登录(不确定这是否意味着创建一个身份验证票据或诸如此类的东西)。实际登录用户的下一步是什么,我不希望它们被重定向到任何地方,因为它们已经在正确的页面上。

I would also like to be able to retrieve their user name or user id later on for use in my web services. So, for this should I do a Session.Add to create a new session value or is there some other way of storing the data that is preferred?

我还希望以后能够检索他们的用户名或用户ID,以便在我的Web服务中使用。那么,为此,我应该创建一个Session.Add来创建一个新的会话值,还是有其他方式来存储首选的数据?

3 个解决方案

#1


For authenticating the user,

为了验证用户,

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

This logs the user in. You can later use

这会将用户登录。您可以在以后使用

Page.User.Identity.Name

to retrieve username of the current user and

检索当前用户的用户名和

Page.User.Identity.IsAuthenticated

to check if the user is logged in.

检查用户是否已登录。

#2


There's no need to store it in Session. Just use:

没有必要将它存储在Session中。只需使用:

FormsAuthentication.SetAuthCookie

to send an authentication ticket to the client. Then use HttpContext.Current.User.Identity to retrieve it later.

将身份验证票证发送到客户端。然后使用HttpContext.Current.User.Identity稍后检索它。

#3


I find using the membership provider is useful, I would recommend it

我发现使用会员提供商是有用的,我会推荐它

Scott Guthrie posted great blog on this

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

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

#1


For authenticating the user,

为了验证用户,

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

This logs the user in. You can later use

这会将用户登录。您可以在以后使用

Page.User.Identity.Name

to retrieve username of the current user and

检索当前用户的用户名和

Page.User.Identity.IsAuthenticated

to check if the user is logged in.

检查用户是否已登录。

#2


There's no need to store it in Session. Just use:

没有必要将它存储在Session中。只需使用:

FormsAuthentication.SetAuthCookie

to send an authentication ticket to the client. Then use HttpContext.Current.User.Identity to retrieve it later.

将身份验证票证发送到客户端。然后使用HttpContext.Current.User.Identity稍后检索它。

#3


I find using the membership provider is useful, I would recommend it

我发现使用会员提供商是有用的,我会推荐它

Scott Guthrie posted great blog on this

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

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