如何在ASP中访问UserId。不使用Membership. getuser()的净成员资格?

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

How can I access UserId in ASP.NET Membership without using Membership.GetUser(username) in ASP.NET Web Application Project?

如何在ASP中访问UserId。在ASP中不使用Membership. getuser(用户名)的净成员资格。净的Web应用程序项目吗?

Can UserId be included in Profile namespace next to UserName (System.Web.Profile.ProfileBase)?

可以将UserId包含在用户名(System.Web.Profile.ProfileBase)旁边的配置文件名称空间中吗?

10 个解决方案

#1


22  

Try this:

试试这个:

MembershipUser CurrentUser = Membership.GetUser(User.Identity.Name);
Response.Write("CurrentUser ID :: " + CurrentUser.ProviderUserKey);

#2


13  

Is your reason for this to save a database call everytime you need the UserId? If so, when I'm using the ASP.NET MembershipProvider, I usually either do a custom provider that allows me to cache that call, or a utility method that I can cache.

您这样做是为了在每次需要UserId时保存数据库调用吗?如果是的话,当我使用ASP的时候。NET MembershipProvider,我通常会做一个自定义提供程序,它允许我缓存那个调用,或者一个我可以缓存的实用程序方法。

If you're thinking of putting it in the Profile, I don't see much reason for doing so, especially as it also will still require a database call and unless you are using a custom profile provider there, it has the added processing of parsing out the UserId.

如果您想把它放在概要文件中,我不认为有什么理由这样做,特别是因为它仍然需要一个数据库调用,除非您使用的是自定义概要文件提供程序,它具有解析出UserId的附加处理。

If you're wondering why they did not implement a GetUserId method, it's simply because you're not always guaranteed that that user id will be a GUID as in the included provider.

如果您想知道为什么它们没有实现GetUserId方法,那只是因为您并不总是保证该用户id将是包含在包含的提供者中的GUID。

EDIT:

编辑:

See ScottGu's article on providers which provides a link to downloading the actual source code for i.e. SqlMembershipProvider.

请参阅ScottGu的文章,该文章提供了下载实际源代码的链接,即SqlMembershipProvider。

But the simplest thing to do really is a GetUserId() method in your user object, or utility class, where you get the UserId from cache/session if there, otherwise hit the database, cache it by username (or store in session), and return it.

但是最简单的方法是用户对象或实用程序类中的GetUserId()方法,在这个方法中,您可以从缓存/会话中获得UserId(如果有的话),否则就访问数据库,按用户名(或会话中的存储)缓存它,然后返回它。

For something more to consider (but be very careful because of cookie size restrictions): Forms Auth: Membership, Roles and Profile with no Providers and no Session

对于需要考虑的更多内容(但由于cookie大小限制,要非常小心):form Auth:成员资格、角色和概要,没有提供者和会话

#3


4  

I decided to write authentication of users users on my own (very simple but it works) and I should done this long time ago.

我决定自己编写用户用户的身份验证(非常简单,但很有效),我早就应该这么做了。

My original question was about UserId and it is not available from:

我最初的问题是关于UserId的,无法从:

System.Web.HttpContext.Current.User.Identity.Name

#4


2  

Try the following:

试试以下:

Membership.GetUser().ProviderUserKey

#5


1  

public string GetUserID()
            {
                MembershipUser _User;
                string _UserId = "";
                _User = Membership.GetUser();
                Guid UserId = (Guid)_User.ProviderUserKey;
                return _UserId = UserId.ToString();
            }

#6


0  

You have two options here:

你有两个选择:

1) Use username as the primary key for your user data table i.e:

1)使用用户名作为用户数据表的主键,即:

select * from [dbo.User] where Username = 'andrew.myhre'

2) Add UserID to the profile.

2)向配置文件中添加UserID。

There are pros and cons to each method. Personally I prefer the first, because it means I don't necessarily need to set up the out-of-the-box profile provider, and I prefer to enforce unique usernames in my systems anyway.

每种方法都有优缺点。我个人更喜欢第一个,因为这意味着我不需要设置开箱即用的配置文件提供程序,而且我更喜欢在我的系统中强制唯一的用户名。

#7


0  

Andrew: I'd be careful of doing a query like what you've shown as by default, there's no index that matches with that so you run the risk of a full table scan. Moreover, if you're using your users database for more than one application, you haven't included the application id.

Andrew:我很小心,像你默认的那样做一个查询,没有与之匹配的索引,所以你会有一个全表扫描的风险。此外,如果您在多个应用程序中使用用户数据库,则没有包含应用程序id。

The closest index is aspnet_Users_Index which requires the ApplicationId and LoweredUserName.

最近的索引是aspnet_Users_Index,它需要ApplicationId和小写的用户名。

EDIT:

编辑:

Oops - reread Andrew's post and he's not doing a select * on the aspnet_Users table, but rather, a custom profile/user table using the username as the primary key.

噢,再读一下Andrew的文章,他并没有在aspnet_Users表上执行select *,而是使用用户名作为主键的自定义配置文件/用户表。

#8


0  

I had this problem, the solution is in the web.config configuration, try configuring web.config with these:

我有这个问题,解决办法在网上。配置配置,尝试配置web。与这些配置:

 <roleManager
             enabled="true"
             cacheRolesInCookie="true"
             defaultProvider="QuickStartRoleManagerSqlProvider"
             cookieName=".ASPXROLES"
             cookiePath="/"
             cookieTimeout="30"
             cookieRequireSSL="false"
             cookieSlidingExpiration="true"
             createPersistentCookie="false"
             cookieProtection="All">
  <providers>
    <add name="QuickStartRoleManagerSqlProvider"
        type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        connectionStringName="ASPNETDB"
        applicationName="SecurityQuickStart"/>
  </providers>
</roleManager>

#9


0  

{
MembershipUser m = Membership.GetUser();
Response.Write("ID: " + m.ProviderUserKey.ToString());
}

Will give you the UserID (uniqueidentifier) for the current user from the aspnet_Membership table - providing the current has successfully logged in. If you try to <%= %> or assign that value before a successful authentication you will get the error "Object reference not set to an instance of an object".

将从aspnet_Membership表为当前用户提供UserID (uniqueidentifier)—提供当前用户已成功登录。如果您尝试<%= %>或在成功的身份验证之前分配该值,您将获得错误“对象引用未设置为对象的实例”。

http://www.tek-tips.com/viewthread.cfm?qid=1169200&page=1

http://www.tek-tips.com/viewthread.cfm?qid=1169200&page=1

#10


0  

Have you tried using System.Web.HttpContext.Current.User.Identity.Name? (Make sure to verify that User and Identity are non-null first.)

您尝试过使用System.Web.HttpContext.Current.User.Identity.Name吗?(确保首先验证用户和标识是非空的。)

#1


22  

Try this:

试试这个:

MembershipUser CurrentUser = Membership.GetUser(User.Identity.Name);
Response.Write("CurrentUser ID :: " + CurrentUser.ProviderUserKey);

#2


13  

Is your reason for this to save a database call everytime you need the UserId? If so, when I'm using the ASP.NET MembershipProvider, I usually either do a custom provider that allows me to cache that call, or a utility method that I can cache.

您这样做是为了在每次需要UserId时保存数据库调用吗?如果是的话,当我使用ASP的时候。NET MembershipProvider,我通常会做一个自定义提供程序,它允许我缓存那个调用,或者一个我可以缓存的实用程序方法。

If you're thinking of putting it in the Profile, I don't see much reason for doing so, especially as it also will still require a database call and unless you are using a custom profile provider there, it has the added processing of parsing out the UserId.

如果您想把它放在概要文件中,我不认为有什么理由这样做,特别是因为它仍然需要一个数据库调用,除非您使用的是自定义概要文件提供程序,它具有解析出UserId的附加处理。

If you're wondering why they did not implement a GetUserId method, it's simply because you're not always guaranteed that that user id will be a GUID as in the included provider.

如果您想知道为什么它们没有实现GetUserId方法,那只是因为您并不总是保证该用户id将是包含在包含的提供者中的GUID。

EDIT:

编辑:

See ScottGu's article on providers which provides a link to downloading the actual source code for i.e. SqlMembershipProvider.

请参阅ScottGu的文章,该文章提供了下载实际源代码的链接,即SqlMembershipProvider。

But the simplest thing to do really is a GetUserId() method in your user object, or utility class, where you get the UserId from cache/session if there, otherwise hit the database, cache it by username (or store in session), and return it.

但是最简单的方法是用户对象或实用程序类中的GetUserId()方法,在这个方法中,您可以从缓存/会话中获得UserId(如果有的话),否则就访问数据库,按用户名(或会话中的存储)缓存它,然后返回它。

For something more to consider (but be very careful because of cookie size restrictions): Forms Auth: Membership, Roles and Profile with no Providers and no Session

对于需要考虑的更多内容(但由于cookie大小限制,要非常小心):form Auth:成员资格、角色和概要,没有提供者和会话

#3


4  

I decided to write authentication of users users on my own (very simple but it works) and I should done this long time ago.

我决定自己编写用户用户的身份验证(非常简单,但很有效),我早就应该这么做了。

My original question was about UserId and it is not available from:

我最初的问题是关于UserId的,无法从:

System.Web.HttpContext.Current.User.Identity.Name

#4


2  

Try the following:

试试以下:

Membership.GetUser().ProviderUserKey

#5


1  

public string GetUserID()
            {
                MembershipUser _User;
                string _UserId = "";
                _User = Membership.GetUser();
                Guid UserId = (Guid)_User.ProviderUserKey;
                return _UserId = UserId.ToString();
            }

#6


0  

You have two options here:

你有两个选择:

1) Use username as the primary key for your user data table i.e:

1)使用用户名作为用户数据表的主键,即:

select * from [dbo.User] where Username = 'andrew.myhre'

2) Add UserID to the profile.

2)向配置文件中添加UserID。

There are pros and cons to each method. Personally I prefer the first, because it means I don't necessarily need to set up the out-of-the-box profile provider, and I prefer to enforce unique usernames in my systems anyway.

每种方法都有优缺点。我个人更喜欢第一个,因为这意味着我不需要设置开箱即用的配置文件提供程序,而且我更喜欢在我的系统中强制唯一的用户名。

#7


0  

Andrew: I'd be careful of doing a query like what you've shown as by default, there's no index that matches with that so you run the risk of a full table scan. Moreover, if you're using your users database for more than one application, you haven't included the application id.

Andrew:我很小心,像你默认的那样做一个查询,没有与之匹配的索引,所以你会有一个全表扫描的风险。此外,如果您在多个应用程序中使用用户数据库,则没有包含应用程序id。

The closest index is aspnet_Users_Index which requires the ApplicationId and LoweredUserName.

最近的索引是aspnet_Users_Index,它需要ApplicationId和小写的用户名。

EDIT:

编辑:

Oops - reread Andrew's post and he's not doing a select * on the aspnet_Users table, but rather, a custom profile/user table using the username as the primary key.

噢,再读一下Andrew的文章,他并没有在aspnet_Users表上执行select *,而是使用用户名作为主键的自定义配置文件/用户表。

#8


0  

I had this problem, the solution is in the web.config configuration, try configuring web.config with these:

我有这个问题,解决办法在网上。配置配置,尝试配置web。与这些配置:

 <roleManager
             enabled="true"
             cacheRolesInCookie="true"
             defaultProvider="QuickStartRoleManagerSqlProvider"
             cookieName=".ASPXROLES"
             cookiePath="/"
             cookieTimeout="30"
             cookieRequireSSL="false"
             cookieSlidingExpiration="true"
             createPersistentCookie="false"
             cookieProtection="All">
  <providers>
    <add name="QuickStartRoleManagerSqlProvider"
        type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        connectionStringName="ASPNETDB"
        applicationName="SecurityQuickStart"/>
  </providers>
</roleManager>

#9


0  

{
MembershipUser m = Membership.GetUser();
Response.Write("ID: " + m.ProviderUserKey.ToString());
}

Will give you the UserID (uniqueidentifier) for the current user from the aspnet_Membership table - providing the current has successfully logged in. If you try to <%= %> or assign that value before a successful authentication you will get the error "Object reference not set to an instance of an object".

将从aspnet_Membership表为当前用户提供UserID (uniqueidentifier)—提供当前用户已成功登录。如果您尝试<%= %>或在成功的身份验证之前分配该值,您将获得错误“对象引用未设置为对象的实例”。

http://www.tek-tips.com/viewthread.cfm?qid=1169200&page=1

http://www.tek-tips.com/viewthread.cfm?qid=1169200&page=1

#10


0  

Have you tried using System.Web.HttpContext.Current.User.Identity.Name? (Make sure to verify that User and Identity are non-null first.)

您尝试过使用System.Web.HttpContext.Current.User.Identity.Name吗?(确保首先验证用户和标识是非空的。)