views:

840

answers:

2

I'm writing a new application in ASP.NET MVC. I've created a custom MembershipProvider that stores membership data in my own db schema. It all works, but how do I get the MembershipUser in my application, such that I can get the user key of the logged-on user and load model classes relating to that user?

+3  A: 

You can use the following:

using System.Web.Security;

var user = Membership.GetUser();
Richard
+2  A: 

Use the static Membership class to retrieve the user using GetUser. You'll need to configure your provider in the web.config file. On logon you get the username from, presumably, a text box on your form. Once logged on you can get it from the controller's User property.

string username = this.User.Identity.Name;
MembershipUser user = Membership.GetUser( username );
tvanfosson
I've subsequently found that you don't need the username because Membership.GetUser() just returns the current logged-on user.
Neil Barnwell