Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UserId in MVC to use in View

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!

like image 840
Carsten Oppitz Avatar asked Sep 07 '25 15:09

Carsten Oppitz


2 Answers

@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.

like image 126
Chris Pratt Avatar answered Sep 10 '25 06:09

Chris Pratt


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>
like image 27
Shamseer K Avatar answered Sep 10 '25 07:09

Shamseer K