4

I have the following problem in my MVC 5 web app. I used database first approach and the entity framwork to create a table [accountlist] that has a foreign key relationship on [UserId] to the [Id] column from [AspNetUsers]. Right now the pertaining accountlist.cshtml contains a dropdown menu. I also tried @Html.HiddenFor(model => model.UserId, new { @Value = HttpContext.Current.User.Identity.Name}) but the problem is how that I need the correct userid and not the username (I don't want the [UserName] from [AspNetUsers] to be an primary key).

Unfortunately, HttpContext.Current.User.Identity.GetUserId() as suggested in this solution says that Identity contains no definition for GetUserId().

Does anyone has solution?

Thanks in advance!

Community
  • 1
  • 1
Carsten Oppitz
  • 132
  • 2
  • 11
  • How about this: `Thread.CurrentPrincipal.Identity.GetUserId()`? – Tân Sep 13 '16 at 16:40
  • 3
    `GetUserId` is an extension method... you might need to import the corresponding namespace. See [this question](http://stackoverflow.com/questions/22624470/get-current-user-id-in-asp-net-identity-2-0) – stephen.vakil Sep 13 '16 at 16:52

4 Answers4

5

@stephen.vakil is correct. You need to import the namespace. Specifically, you need to add:

@using Microsoft.AspNet.Identity
@model Namespace.To.Foo

Alternatively, if you want to do this more often or just don't want to include the namespace in the view, you can edit the Web.config in the Views folder of your project and add it there instead:

<?xml version="1.0"?>
<configuration>
  ...
  <system.web.webPages.razor>
    ...
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="Microsoft.AspNet.Identity" />

Any namespaces there are available to every view automatically.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
1

this is the best Article for asp.net mvc authentication from ben foster. It helped me last time.

It works even if you are not going for entity framework

SigIn Like this

var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimTypes.Sid, userID)
            },    
            "ApplicationCookie");

        var ctx = Request.GetOwinContext();
        var authManager = ctx.Authentication;

        authManager.SignIn(identity);

Create New Class

public class AppUser : ClaimsPrincipal
{
    public AppUser(ClaimsPrincipal principal)
        : base(principal)
    {
    }

    public int ID
    {
        get
        {
            return this.FindFirst(ClaimTypes.Sid).Value;
        }
    }   
}

Let's create a custom base view page for our Razor views that provides access to our AppUser principal:

public abstract class AppViewPage<TModel> : WebViewPage<TModel>
{
    protected AppUser CurrentUser
    {
        get
        {
            return new AppUser(this.User as ClaimsPrincipal);
        }
    }
}

public abstract class AppViewPage : AppViewPage<dynamic>
{
}

Open up /views/web.config and the set the pageBaseType:

<system.web.webPages.razor>
  <pages pageBaseType="NakedIdentity.Mvc.AppViewPage">

Rebuild the project and update the Index.cshtml view:

<p>
  User ID: @CurrentUser.ID?
</p>
Shamseer K
  • 4,964
  • 2
  • 25
  • 43
1

If you are using SimpleMemberShip provider, then

int UserId = WebSecurity.CurrentUserId;

and add attribute [InitializeSimpleMembership] on the top of controller method.

Aryan Firouzian
  • 1,940
  • 5
  • 27
  • 41
0

add this line to your controller constructor:

user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

then you will have user.Id in all you methods inside your controller. And for security purpose, never keep your user id inside view as hidden!.

Hadee
  • 1,392
  • 1
  • 14
  • 25