Reputation: 13
I am attempting a simple assignment of a string to the current user ID in a service class like so:
strCurrentUserId = User.Identity.GetUserId();
//My namespace references
using HelpDesk.DAL;
using HelpDesk.Models;
using HelpDesk.ViewModels;
using System;
using Microsoft.AspNet.Identity;
using System.Security.Principal;
Where User.Identity.GetUserId
is accessed via the System.Security.Principal
class.
Visual Studio keeps thinking I am trying to access a model type User
that I do have in my project by this is not what I am trying to call.
This exact line of code works in another class, but not this one and I don't know why.
Upvotes: 0
Views: 3898
Reputation: 119186
User
is a property of the Controller
class which is why you cannot 'see' it from another class.
You have a couple of options:
Pass the User
value to your other class, for example:
public class MyClass
{
public void SomeMethod(System.Security.Principal.IPrincipal user)
{
//do something with 'user'
}
}
public ActionResult SomeAction()
{
var myClass = new MyClass();
myClass.SomeMethod(User);
}
You can also retrieve the user from the HttpContext
. The HttpContext
class has a static method to get the current context. For example:
var user = HttpContext.Current.User;
Upvotes: 3