在新的MVC4应用程序获取用户ID

在新的MVC4应用程序获取用户ID

本文介绍了在新的MVC4应用程序获取用户ID(INT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新MVC4应用程序创建的用户配置表:

New MVC4 application created UserProfile table :

用户ID(INT)|用户名(NVARCHAR)

UserId(int) | UserName(nvarchar)

在控制器:

  string currentUser = User.Identity.Name;  // returns UserName

  var mu1 = Membership.GetUser(); // returns null

  var mu2 = Membership.GetUser(currentUser); // returns null as well

我读了很多线程,他们都谈得到的Guid,我甚至不拥有用户和会员的表格。

I read a lot of threads and they all talk about getting Guid, which I don't even have in user and membership tables.

有没有办法让用户ID(INT)的当前登录的用户?

Is there a way to get the UserId (int) of currently logged in User ?

推荐答案

请确保您 [InitializeSimpleMembership] 添加到您的控制器的顶部,使得它初始化简单会员通过在过滤器文件 InitializeSimpleMembershipAttribute.cs 目录

Make sure you add [InitializeSimpleMembership] to the top of your controller so that it initializes the simple membership via the file InitializeSimpleMembershipAttribute.cs in the Filters directory.

然后就可以访问该用户ID几种不同的方式:

Then you can access the user id in several different ways:

int mu1 = (int)WebSecurity.CurrentUserId;
int mu2 = (int)Membership.GetUser().ProviderUserKey;

这篇关于在新的MVC4应用程序获取用户ID(INT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 10:40