获取当前用户ASP.NET成员的ID [英] Get the ID of the current user ASP.NET Membership

查看:346
本文介绍了获取当前用户ASP.NET成员的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们如何设法得到使用ASP.NET成员资格提供当前用户的ID?我的应用程序是一个MVC 3使用数据库优先的办法,所以我创建了自己的数据库和成员资格提供程序和角色提供使用我的表(自定义成员资格和角色提供)。

How do you guys manage to get the ID of the current user using the ASP.NET Membership Provider? My application is an MVC 3 using Database First approach, so I created my own database and the Membership Provider and Role Provider are using my tables (Custom Membership and Role Provider).

在我的网页(反馈),用户可以将反馈发送关于我的网站,如果他接,我想包括在表中的用户名时,我插入之一。

In one of my page (feedback), user can send feedback about my website and if he's connected, I want to include the UserID in the table when I insert.

但由于ASP.NET成员是的IIdentity,我可以访问的唯一的事情就是名称。我不希望使用的名称,以创建2个表之间的链接,我想ID!

But since the ASP.NET Membership is an IIdentity, the only thing I can access is the Name. I don't want to create a link between 2 tables using the name, I want the ID!

那么你在应用这个非常简单的任务吗?我的意思是,在一个连接的用户需要插入的值几乎每一页,我们需要用户的ID来建立正确的外键的用户表,对不对?顺便说一句,我很新的这MVC框架,我用ASP.NET ASPX和用户信息,我用的是需要这给了我目前的ID,姓名等领域的会议之前。由于使用Session对象,这实现了我的麻烦在共享主机,我想用真正的会员。

So what do you do in your application for this VERY simple task? I mean, in almost every page where a connected user needs to insert a value, we need the ID of the user to create the correct foreign key to the user table, right? By the way, I'm very new to this MVC framework, before that I was using ASP.NET aspx and for the user information, I was using the Session which gave me the current ID, Name and other fields as needed. Since this implementation using the Session object gave me trouble in shared hosting, I want to use the real membership.

谢谢!

修改

我想必须是整数的ID(其实我想要的用户ID字段是在用户表)。

The ID I want must be an integer (in fact, I want the UserID field that is in the User table).

推荐答案

试试这个;

MembershipUser mu = Membership.GetUser("username");
string userId = mu.ProviderUserKey.ToString();

有关当前登录的用户,你可以不经过用户名使用;

For currently logged in user you could use without passing the username;

string userId = Membership.GetUser().ProviderUserKey.ToString();

这篇关于获取当前用户ASP.NET成员的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆