如何获取登录asp.net mvc的用户的userId或membershipId

时间:2021-10-17 06:24:49

I'm working on a Employer-worker website. My problem is whenever a specific employer is log in I want to filter all the position that certain employer have and put it in a dropdown. How can I get the userId of the current user for me be able to filter the position.

我正在雇主 - 工人网站上工作。我的问题是,只要特定的雇主登录,我想过滤某个雇主所拥有的所有职位并将其放入下拉列表中。如何让我的当前用户的userId能够过滤位置。

In the database of the employer I have a field named userId it a FK of the userId in my membershipId table.

在雇主的数据库中,我有一个名为userId的字段,它是我的membershipId表中userId的FK。

I found this on the net:

我在网上发现了这个:

Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;

But I can't understand how can I use or put this on my controller.

但是我无法理解如何在控制器上使用或放置它。

2 个解决方案

#1


1  

Try this:

尝试这个:

//simplified version
MembershipUser membershipUser = Membership.GetUser();
string UserID = membershipUser.ProviderUserKey.ToString();

The way you found is also correct, but the questions in where you want to use this in controller.

你找到的方式也是正确的,但你想在控制器中使用它的问题。

Most common uses are like redirecting the users to the corresponding view, which is accessible to the current user.

最常见的用途是将用户重定向到相应的视图,当前用户可以访问该视图。

#2


0  

You can use

您可以使用

User.Identity.Name

inside your controller

在你的控制器内

#1


1  

Try this:

尝试这个:

//simplified version
MembershipUser membershipUser = Membership.GetUser();
string UserID = membershipUser.ProviderUserKey.ToString();

The way you found is also correct, but the questions in where you want to use this in controller.

你找到的方式也是正确的,但你想在控制器中使用它的问题。

Most common uses are like redirecting the users to the corresponding view, which is accessible to the current user.

最常见的用途是将用户重定向到相应的视图,当前用户可以访问该视图。

#2


0  

You can use

您可以使用

User.Identity.Name

inside your controller

在你的控制器内