0

I'm new to MVC and have a question. The Visual Studio template creates a table dbo.AspNetUsers that has a column called Id. From my controller I want to get that value for the user who is signed in

    public ActionResult Index ()
    {

        if (!Request.IsAuthenticated) // if not logged in 
        {
            Response.StatusCode = 404;
            return null;
        }
        else
        {
             string thisUserId = ???
             // do something with thisUserId
        }

I've looked through the source files but can't figure out how I get this.

ataravati
  • 8,891
  • 9
  • 57
  • 89
user4905335
  • 371
  • 2
  • 13

1 Answers1

1

This works in my project (which uses the System.Web.Security.SqlMembershipProvider)

HttpContext httpContext = HttpContext.Current;
string userName = httpContext.User.Identity.Name;
MembershipUser membershipUser = Membership.GetUser(userName);
object providerUserKey = membershipUser.ProviderUserKey;
string thisUserId = providerUserKey.ToString();
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
  • The `userId` is of type `nvarchar`.Why are you casting it to a `Guid` and back into a `string` again? – user4905335 May 21 '15 at 00:56
  • I suspect I'm using an earlier version of Asp.NET Membership then you, and this is storing the UserId as a `uniqueidentifier`. I'll take that part out of the code example. – Andrew Shepherd May 21 '15 at 01:00