ASP.NET:如何在没有调用Membership.GetUser()的情况下获取当前用户?

时间:2021-07-25 03:41:10

How do I get the current user without call Membership.GetUser()? I don't want to call this method because it makes a round-trip to the database.

如何在没有调用Membership.GetUser()的情况下获取当前用户?我不想调用此方法,因为它会对数据库进行往返。

I tried Page.User, but this doesn't return a MembershipUser object.

我尝试过Page.User,但这不会返回MembershipUser对象。

3 个解决方案

#1


3  

If you just need the user's username, then just call User.Identity.Name. If you need other information about the MembershipUser, then you'll can't avoid that trip to database (unless as CResults pointed out, store the object in session).

如果您只需要用户的用户名,那么只需调用User.Identity.Name即可。如果您需要有关MembershipUser的其他信息,那么您将无法避免该数据库之旅(除非CResults指出,将对象存储在会话中)。

#2


1  

Have you considered storing it in an encrypted state in your session object?

您是否考虑过在会话对象中以加密状态存储它?

Call GetUser once, store the result in the session and from then on get the value from the session object.

调用GetUser一次,将结果存储在会话中,然后从会话对象中获取值。

#3


1  

when user is logged in. you can User.Identity.Name . you can check with if(User.Identity.IsAuthenticated) and User.Identity.Name. This might help you.

当用户登录时,您可以使用User.Identity.Name。您可以查看if(User.Identity.IsAuthenticated)和User.Identity.Name。这可能对你有帮助。

#1


3  

If you just need the user's username, then just call User.Identity.Name. If you need other information about the MembershipUser, then you'll can't avoid that trip to database (unless as CResults pointed out, store the object in session).

如果您只需要用户的用户名,那么只需调用User.Identity.Name即可。如果您需要有关MembershipUser的其他信息,那么您将无法避免该数据库之旅(除非CResults指出,将对象存储在会话中)。

#2


1  

Have you considered storing it in an encrypted state in your session object?

您是否考虑过在会话对象中以加密状态存储它?

Call GetUser once, store the result in the session and from then on get the value from the session object.

调用GetUser一次,将结果存储在会话中,然后从会话对象中获取值。

#3


1  

when user is logged in. you can User.Identity.Name . you can check with if(User.Identity.IsAuthenticated) and User.Identity.Name. This might help you.

当用户登录时,您可以使用User.Identity.Name。您可以查看if(User.Identity.IsAuthenticated)和User.Identity.Name。这可能对你有帮助。