Julie Røsok
Julie Røsok

Reputation: 109

getUserId does not exist in User.Identity in rc1.final

I am using asp.net 5 and entity framework 7 to create a web application, and I am having some issues with getting User.Identity to work. In the documentation, it says it lies in the namespace User.Identity.Core. When I install this via nuget, it finds the method. However, I get conflicts with UserManager, as it says it exists in both User.Identity.Core and User.Identity.EntityFramework. If i uninstall User.Identity.EntityFramework, I am not allowed to inherit from IdentityUser in my User-model. I need the GetUserId in order to get the current logged inn user... I've also tried to use

System.Web.HttpContext.Current.User.Identity.GetUserId(); 

But there seems to be a similar problem, where it says Current does not exist in HttpContext.

Does anyone know a solution to this?

Update with project.json:

{
  "userSecretsId": "aspnet5-FrkKantine-1a8100b9-6fce-44b4-ac9d-6038ecf705f6",
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Razor": "4.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": { },
    "dnx50": {}
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
  }
}

Upvotes: 3

Views: 4934

Answers (3)

João Pereira
João Pereira

Reputation: 1667

If you are using .Net Core <= RC1 Update 1, you still can use the GetUserId() extension.

References required:

using Microsoft.AspNet.Identity;
using System.Security.Claims;

Usage: var id = User.GetUserId(); (and not User.Identity.GetUserId())

Another different thing you mention is about HttpContext.Current being null, from which I assume you are trying to access the user from a class library or outside the MVC's scope.

If you need to access the HttpContext from a class library you can't just access it like a static property. It is required to be injected in that class.

Just do the following:

private readonly IHttpContextAccessor _httpContextAccessor;

public MyRepository(IHttpContextAccessor httpContextAccessor)
{
    _httpContextAccessor = httpContextAccessor;
}

Then you can use it through all the methods in the class like this:

_httpContextAccessor.HttpContext.User.GetUserId();

Upvotes: 0

Tseng
Tseng

Reputation: 64278

Since ASP.NET Core 1.0 (former ASP.NET 5) is still in development, things change often.

Please check out the GitHub Announcements tracker for API changes the ASP.NET Core 1.0 releases (don't post issues there though, it's purely for the ASP.NET Core team to post announcements).

One of these announcements is about the moving of GetUsername, GetUserId and IsSignedIn extension methods.

The claims that these extension methods refer to are configurable via IdentityOptions which was not currently being respected by the extensions methods which always referred to the build in ClaimTypes.NameIdentifier/Name.

These methods have been moved to address this: Before => After

User.GetUserId => UserManager.GetUserId(User)
User.GetUserName => UserManager.GetUserName(User)
User.IsSignedIn => SignInManager.IsSignedIn(User)

edit: I just noticed it's for RC2, so likely not yet apply to you unless you use nightly builds feed.

Your issue may come from choosing the wrong package names, as they have been renamed a half dozen of times in the past

EntityFramework.MicrosoftSqlServer and Microsoft.AspNet.Identity.EntityFramework should be the ones for you. (with RC2 they got another rename to Microsoft.EntityFrameworkCore.* and Microsoft.AspNetCore.* and all versions reset to 1.0)

Upvotes: 7

Firas Maswadeh
Firas Maswadeh

Reputation: 39

Solution 1:

Try This :

using Microsoft.AspNet.Identity;

and then :

string userId = User.Identity.GetUserId();

Solution 2 :

usually you should make sure if the user Authenticated and then get the user info / id :

if (model != null)
    {
        model.IsUserAuthenticated = HttpContext.User.Identity.IsAuthenticated;

        if (model.IsUserAuthenticated)
        {
            model.UserName = HttpContext.User.Identity.Name;
        }
    }

Upvotes: 0

Related Questions