How to find current logged in user in DotNetNuke

While developing DotNetNuke modules a lot of time you need to add features that have role based access control. To authorize the access, you will need the following set of information about the current user.

  • Is it authenticated session or anonymous user?
  • If its authenticated, who is the user?
  • What role(s) this user belongs to?
  • And other permissions related about information about the user

If you are not very familiar with DNN core API, then getting this information is not very obvious. And lack on online documentation of DNN API kind of makes it hard to search API as well. The API is very well done and has appropriate method names. But if there was an index, then it will be just quickly searching and getting the information.

Anyways, in DNN core, look in DotNetNuke.Entities.Users namespace. You will find two very useful classes UserController and UserInfo classes. As you may have guessed, these are two objects that you need to get user information. UserController has a static/friend method GetCurrentUserInfo that returns a UserInfo object. You can inspect this returned object to get more details about the user. For example, following code snippet shows how to find current logged in user in DNN and if this user is in particular role or not.

Dim userInfo = UserController.GetCurrentUserInfo()
if (userInfo.IsInRole("Manager")) Then
' Do some manager related work here
End If

You can explore UserInfo object to see what all you can do with it. Here is another example code that shows how to find all the roles of a user in DNN.

Dim userInfo = UserController.GetCurrentUserInfo()
Dim userRoles = userInfo.Roles

As you can see, the method names are very intutive and implemented at right place. Its just matter of knowing where to look for. If you have any questions about any DNN API, please feel free to drop me a line.




-0.2 °C / 31.6 °F

weather conditions Clouds

Monthly Posts

Blog Tags