1

The problem seems so ridiculous. I am trying to get the id of current logged in user here but get error "The type or namespace name 'User' does not exist in the namespace 'Microsoft.AspNet.Identity' (are you missing an assembly reference?) if I try to access Microsoft.AspNet.Identity.User.GetUserId(); Other properties of the namespace are accessable as you can see here.

Screenshot

I have installed following packages in current project.

Screenshot

Has accessing current user changed in Identity 2.0.1? I have looked at ASP.NET MVC 5 - Identity. How to get current ApplicationUser and from that question this should work. Did not see other properties that would mean the same thing.

The rest of the functionality (creating users, signing in, etc) is working fine. Have been banging my head against the wall for hours. Any help would be appretiated. Thanks.

Community
  • 1
  • 1
chris544
  • 889
  • 2
  • 10
  • 21
  • Your title and body mention asp.net, but you've tagged with asp-classic. – IMSoP May 11 '14 at 14:46
  • Sorry, edited the question (shaky hands I guess). – chris544 May 11 '14 at 14:49
  • Where exactly did you get the idea that `Microsoft.AspNet.Identity.User.GetUserId()` or that `Microsoft.AspNet.Identity.User` are valid things? They don't exist, that's why you can't find them. The link to the article you mention doesn't list this method or object. – Erik Funkenbusch May 11 '14 at 16:52
  • Yeah, User should come from a completely different namespace. Somehow read the comment that using Microsoft.AspNet.Identity; should be added and assumed it must be there. – chris544 May 11 '14 at 17:15

2 Answers2

2

Since you seem to do better with pictures... Here is the solution.

NOTE:, you don't pass anything as a parameter, because this is an extension method. Notice the "this" in the parameter list, that's a big clue. Also notice the icons with the arrow pointing down next to the method name, that also indicates that it's an extension method.

enter image description here

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • This answer is pretty clear :D And kind of a bigger takeaway would be that current User is a part of the UI (if it makes sense). I kind of felt that User just cannot be a part of Page object. – chris544 May 11 '14 at 17:51
  • @chris544 - It's not technically, it's a part of the HttpContext, the Page object just has a convenient reference to it. – Erik Funkenbusch May 11 '14 at 17:54
1

You should probably be using this: Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(). Have a look at the accepted answer on this SO post.

Community
  • 1
  • 1
CopyPasteDev
  • 196
  • 1
  • 14
  • Yes, it makes sense. But I have to pass in "System.Security.Principal.IIdentity" type. Do I have to configure it somewhere? – chris544 May 11 '14 at 15:40
  • Still kind of stuck. I understand that your mentioned method is practically the same as method above, but I cannot access it because I have to pass in IIdentity and it is somehow not passed in automatically. Do I have to add something in web.config file? Maybe some configuration is missing.... – chris544 May 11 '14 at 16:37
  • @chris544 - System.Security.Principal.IIdentity is provided by the User object in the Page class that is the base class of every aspx page. You just call User.Identity.GetUserId(), just like the article you originally linked to says to do (which for some reason, you ignored). GetUserId is an extension class, which adds methods to an object, in this case, the User.Identity object. – Erik Funkenbusch May 11 '14 at 17:06
  • OK, so the problem is clear now. And it began because I tried to access User.Identity.GetUserId() from another project ... that is not a Page. UPDATE: I just wanted to seperate out all user management stuff in a seperate project to create a layer of abstraction. It seems that I will have to just use User.Identity functions in my pages, that seems pretty ugly, but at least it works ... – chris544 May 11 '14 at 17:17
  • 1
    @chris544 - You generally shouldn't be accessing the User identity from other projects. That's not good design. However, if you must you can always access HttpContext.Current.User.Idenity.GetUserId() static method, but that requires that you add various reference dependencies (which is part of the reason it's not good design, it makes your business logic depend on your presentation layer, not the other way around). – Erik Funkenbusch May 11 '14 at 17:24
  • 1
    @chris544 - It's not ugly at all. The reason is that the "current user" is a function of the User Interface. Authentication and Authorization all happen at the presentation level. You can abstract your user management, but just remember that the responsibility ultimately rests with the web pages themselves. If your logic requires the current user, it should be passed to the logic from the UI. – Erik Funkenbusch May 11 '14 at 17:26
  • If I understand correctly we just need to "have" the page we visited to get all the cookies and stuff from is somehow stored in it. It is not like session variables that can be accessed from everywhere Seems pretty straightforward. I could just pass the current Page to my IdentityManager class if I really want to seperate something (pretty pointless). Thanks for your help. Maybe you could update or add a new answer that I could accept (if you want to). So far the question and answer are quite confusing and they might just frustrate other people in the future. – chris544 May 11 '14 at 17:31
  • @chris544 - If your code is going to be dependent upon asp.net, then you can just pass the IIdentity (ie User.Identity object), or if you need the IPrincipal you can just pass the User object. However, I question the value of making it a separate project if you're just going to make it depend on Asp.net, unless you plan to reuse this code in other web apps. I think the answer I gave is pretty straight forward. – Erik Funkenbusch May 11 '14 at 17:43
  • Yeah, to keep my business logic seperated I probably will use someting like IdentityManager.ChangePassword(User.Identity.GetUserId(), oldPassword, newPassword) in my UI project to call methods that are in my BusinessLogic project. And if I need to pass them in often, I can extend BasePage to include "shortcuts" to CurrentUserId, CurrentUserName etc. Obviously. Again huge thanks for your help, some pretty nice insights here. – chris544 May 11 '14 at 18:12