Neil Barnwell
Neil Barnwell

Reputation: 42165

Obtain MembershipUser in ASP.NET MVC

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?

Upvotes: 6

Views: 5385

Answers (2)

Richard
Richard

Reputation: 22036

You can use the following:

using System.Web.Security;

var user = Membership.GetUser();

Upvotes: 9

tvanfosson
tvanfosson

Reputation: 532755

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 );

Upvotes: 4

Related Questions