Welcome to Dream.In.Code
Become an Expert!

Join 149,457 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,141 people online right now. Registration is fast and FREE... Join Now!




Accessing User Identity In Class Library

 
Reply to this topicStart new topic

Accessing User Identity In Class Library, What are your thoughts on accessing user identities from a class libra

mattw
14 Oct, 2007 - 10:58 PM
Post #1

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 23


My Contributions
Hey All,

This is a fairly typical situation. I have started a new ASP.Net Application with a corresponding class library project. Now, it's pretty common to identify your users on a given asp.net page by using the following code:

CODE


'Get user identity from iprincipal
Dim mUserName As String = Page.User.Identity.Name



However when you're in a library project the same approach is not available as you a no longer using the concept of a page class to access the user's identity from.

My question is: Aside from continually relaying the users id (or a user object) to your library methods, has anyone come across a better way of accessing the current user's id from within the library project? I'm interested in peoples different approaches!

Thanks in advance!
User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: Accessing User Identity In Class Library
14 Oct, 2007 - 11:07 PM
Post #2

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
I personally don't know much in the way of ASP.NET (at least on this level, I tend to lean towards PHP for some reason when developing web sites), but it seems like you could add a Imports System.Web.UI line at the top of the class to add a new reference to the Web.UI portion of system where the Page class is stored.

This post has been edited by aceofspades686: 14 Oct, 2007 - 11:07 PM
User is offlineProfile CardPM
+Quote Post

mattw
RE: Accessing User Identity In Class Library
14 Oct, 2007 - 11:09 PM
Post #3

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 23


My Contributions
QUOTE(aceofspades686 @ 15 Oct, 2007 - 12:07 AM) *

I personally don't know much in the way of ASP.NET (at least on this level, I tend to lean towards PHP for some reason when developing web sites), but it seems like you could add a Imports System.Web.UI line at the top of the class to add a new reference to the Web.UI portion of system where the Page class is stored.


Hey Ace,

Thanks for responding. Granted your approach would give me access to the actual namespaces and classes but wouldn't give me access to the users identity at runtime.
User is offlineProfile CardPM
+Quote Post

aceofspades686
RE: Accessing User Identity In Class Library
14 Oct, 2007 - 11:12 PM
Post #4

D.I.C Regular
Group Icon

Joined: 8 Oct, 2007
Posts: 261


Dream Kudos: 100
My Contributions
Haha, alright. As I said, I don't really know ASP.Net that well, but I figured I would take a shot in the dark and see if it was worth anything. If nothing else, I thought you might reply (like you did) and I would learn a little more.

By the way, you probably would've gotten a better response in the ASP.Net forum. wink2.gif

EDIT: Of course, silly me misread the question entirely as well and I just now caught that. I'm actually curious myself as if there would be another way besides the "continuous relay" approach.

This post has been edited by aceofspades686: 14 Oct, 2007 - 11:15 PM
User is offlineProfile CardPM
+Quote Post

mattw
RE: Accessing User Identity In Class Library
14 Oct, 2007 - 11:25 PM
Post #5

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 23


My Contributions
QUOTE(aceofspades686 @ 15 Oct, 2007 - 12:12 AM) *

Haha, alright. As I said, I don't really know ASP.Net that well, but I figured I would take a shot in the dark and see if it was worth anything. If nothing else, I thought you might reply (like you did) and I would learn a little more.

By the way, you probably would've gotten a better response in the ASP.Net forum. wink2.gif

EDIT: Of course, silly me misread the question entirely as well and I just now caught that. I'm actually curious myself as if there would be another way besides the "continuous relay" approach.


Lol not to worry. The question is a bit tricky to place as strictly its more of a VB.Net Library project query, but it does tie in with ASP.Net. I know a load of people continually relay the user id or a custom user object to their library at run time, but I'm just curious also to see if there is a better approach. maybe i'll link to this post from the asp.net forum and see if that helps.
User is offlineProfile CardPM
+Quote Post

mattw
RE: Accessing User Identity In Class Library
15 Oct, 2007 - 12:47 AM
Post #6

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 23


My Contributions
Hey Guys,

I have tried to use threading as this will be a system using windows authentication, does anyone have any thoughts on using the threading approach?

i.e.

CODE

'Obtain username from iprinciple
UserName = Thread.CurrentPrincipal.Identity.Name

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Accessing User Identity In Class Library
15 Oct, 2007 - 12:49 AM
Post #7

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,477



Thanked: 161 times
Dream Kudos: 9025
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I'm a little confused, Ive worked with .Net since it was in Beta mode, I'm certified in both C# and VB.Net (and thus ASP.Net) and having a hard time understanding what a class library project is, with a decent explanation Im sure I can help, but you're really confusing me here with your terminology crazy.gif

Next, if you are referring to you're creating a web application that has a class in it that controls all the data transfer and such then when the user logs in set a session variable, then as long as the user never leaves, and never logs off, you just use Session("YourVariableName")

Now as far as this statement:

QUOTE

maybe i'll link to this post from the asp.net forum and see if that helps.


Do not post the same question in 2 different forums, double posting isn't allowed here at </dream.in.code>.

If you're, as I stated above, using a custom class you've written, then in that class create a public property to hold the users name

CODE

Dim _userName As String

Public Property Username As String
      Get
           Return _userName
      End Get
      Set(ByVal value As String)
          _userName = value
      End Set
End Property


Then when you need that users name (or ID if thats what you're using) then just set the property of you class MyClassObject.UserName = Page.User.Identity.Name, but I think the Session object is probably your best bet.

By the way, this is an ASP.Net question so I'm moving this topic there smile.gif
User is offlineProfile CardPM
+Quote Post

mattw
RE: Accessing User Identity In Class Library
25 Oct, 2007 - 04:04 PM
Post #8

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 23


My Contributions
Hey Psycho,

First of all my apologies for incorrectly posting in both forums, it was a little difficult to place the topic hence opting for both forums, point taken.

To be completely honest with you, seeing you have all the certifications I'm a little astounded the term "Class Library Project" has confused you. This is a standard project type in Visual Studio.

- Visual Basic > Class Library
- Other Languages > Visual C# > Class Library

My question is, with web applications the authentication between application and client typically occurs in the web application project by obtaining their domain id and verifying they have access to relevant parts of the system. When your application then comes to use the Class Library Project (i.e. where you have separated out your business logic) you then have to be able identify the user that has generated the given request (for assigning ownership to data etc).

Typically when identifying the user from a web application to a class library you will generally pass the users domain Id (in one fashion or another). It was this process my original post related to and was querying if anyone used any other practices to achieve identifying users in the class library.

I'm aware of the persistence mechanisms used to store the user's id, i.e. sessions etc, I was more so querying how to identify users when in a class library which wouldn't has access to the http session object.

If all this seems like jibberish then save yourself some time and pass on responding - i was just hoping for some input with people using a similar setup and to share ideas.

Back to the drawing board *sigh*
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Accessing User Identity In Class Library
25 Oct, 2007 - 05:12 PM
Post #9

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,477



Thanked: 161 times
Dream Kudos: 9025
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well, Ive never looked at all the project types available in Visual Studio. Getting a MCSD and MCAD means I know the language, not the IDE. Plus I don't always use Visual Studio, I almost spend more time in Sharp Develop than in Visual Studio, so not knowing theres a "Class Library Project" type really doesn't mean anything smile.gif

I didn't say any gibberish at all, you simply create a public property in your class library, and pass the same value to it you get from Page.User.Identity.Name. What I said in my post is how I would handle the situation, and I continue to stand by the fact that I would handle it that way.

Thats also the way I handle it with my IP filtering component, I simply pass the value from Request.ServerVariables["REMOTE_ADDR"].ToString(), which also isn't available to a non web application, so the same applies as well.

Just because something isn't available to your class object, doesn't mean that if someone suggests passing the value to the object and accept it in the object in the form of a public property doesn't make it gibberish, it makes it an offer for a solution. So, if you don't want to accept my solution thats fine with me, but you don't have to be an a** about it either smile.gif
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Accessing User Identity In Class Library
26 Oct, 2007 - 10:39 AM
Post #10

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,275



Thanked: 135 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(mattw @ 15 Oct, 2007 - 02:58 AM) *
Aside from continually relaying the users id (or a user object) to your library methods, has anyone come across a better way of accessing the current user's id from within the library project?


This a great question. Yes, it is common.

Passing an System.Security.Principal.IIdentity around is not the end of the world, of course. I usually write my own one of these, that also implements System.Security.Principal.IPrincipal. That way my IPrincipal.IsInRole method behaves as expected.

First, to be clear, in ASP.NET world we get IIdentity via System.Web.HttpContext.Current.User.Identity;. For Winforms, the code you want is System.Security.Principal.WindowsIdentity.GetCurrent(); In ASP the Winforms code will give you the IIdentity that the server is running under, which is not normally useful.

Personally, if I have a collection of objects that require identity information, I use a State / Factory style pattern that instantiates all such objects for me. Or wraps method calls so I only have to pass the information once.

Let's assume my class library has a root namespace of AccessLib. I set up a single exposed class tho manage all my operations, like so.

CODE
using System;
using System.Security;
using System.Security.Principal;

namespace AccessLib {
    public abstract class Manager    {
        // We usually want this as a Singleton.
        protected Manager() {}
        public abstract System.Security.Principal.IIdentity CurrentUser { get; }
        public string UserName { get { return this.CurrentUser.Name; } }
...


Now, in order to use any objects in my library, the calling project will need to implement a concrete version. For Windows, this looks like:

CODE
public class AccessManager : AccessLib.Manager {
    private static AccessManager instance = new AccessManager();
    public static AccessManager Instance { get { return instance; } }
    protected AccessManager() { }
    public override System.Security.Principal.IIdentity CurrentUser {
        get { return System.Security.Principal.WindowsIdentity.GetCurrent(); }
    }
}



For ASP, like so.

CODE
public class AccessManager : AccessLib.Manager {
    private static AccessManager instance = new AccessManager();
    public static AccessManager Instance { get { return instance; } }
    protected AccessManager() { }
    public override System.Security.Principal.IIdentity CurrentUser {
        get {
            return System.Web.HttpContext.Current.User.Identity;
        }
    }
}



While it will take a little discipline to implement this style of pattern, I feel the level of abstraction is worth if for maintenance.

Hope this helps.

User is online!Profile CardPM
+Quote Post

sontek
RE: Accessing User Identity In Class Library
26 Oct, 2007 - 03:29 PM
Post #11

D.I.C Regular
Group Icon

Joined: 13 Sep, 2001
Posts: 283



Thanked: 1 times
Dream Kudos: 85
My Contributions
You can use HttpContext.Current.User.Identity to get it, you can also use it to get the session and request properties if you need to access those from a library.
User is offlineProfile CardPM
+Quote Post

mattw
RE: Accessing User Identity In Class Library
29 Oct, 2007 - 10:15 PM
Post #12

New D.I.C Head
*

Joined: 2 Sep, 2007
Posts: 23


My Contributions
Hey All,

First of all let me just get thing straight. Psycho - I wasn't saying what you were suggesting was jibberish, to the contrary I was grateful of your input. I think you misunderstood me, when i said "If all this seems like jibberish then save yourself some time and pass on responding" - I meant if what I am saying seems like jibberish then don't worry about spending time responding. Sorry if it offended you, it was not intended to.

So...

baavgai - Thanks for the information, some really good points there. Seems so clear when somebody puts the answer on the table, but a huge thanks! Was just what I was after. Hopefully other users of Dream.In.Code will benefit from this info too.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 01:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month