Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 469
Neil Barnwell Avatar asked Mar 14 '09 11:03

Neil Barnwell


2 Answers

You can use the following:

using System.Web.Security;

var user = Membership.GetUser();
like image 104
Richard Avatar answered Sep 22 '22 02:09

Richard


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 );
like image 36
tvanfosson Avatar answered Sep 20 '22 02:09

tvanfosson