Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User.Identity.Name within a module? 2

Status
Not open for further replies.

Junior1544

Technical User
Apr 20, 2001
1,267
0
0
US
Is there a way to put User.Identity.Name within a Module??

when I've tried it, VS.Net tells me that Name is:
"Reference to a non-shared member requires an object refernce"

How would I go about making this work??? or should i be using some thing other then a module??

Thanks
--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Hey Junior,

I'm guessing that Name is a field within an Identity object, yes?

If so, then you need to do one of two things:
1. In your module, create an instance of the Identity object (which would allow you to access the Name field
2. If this is somethign being passed to a sub/function, pass in the Name as a parameter
i.e. Public Sub DoSomething(ByVal nameValue as string)
End Sub

Maybe if you could explain a bit more what you're trying to do within the module, we might be able to give a bit more exact help as well.
:)

D'Arcy
 
I'm still just learning how to do every thing, so thank you for the patience...

I can put some thing like this in the aspx.vb file.
textbox1.text = User.Identity.Name
and it will put in there the user name of the person currently logged into the web site...

I use this so that it doesn't include the domain/ in the user name:
UCase(Right(User.Identity.Name, Len(InStr(User.Identity.Name, "/"))))

this will return only the user name and no domain information with it... I am tring to make a public function within a module to allow me just use some thing like
currentuser()
to return the user name, much shorter, and easier to move around...

this is what i have for my funtion:

Public Function CurrentUser() as string
CurrentUser = UCase(Right(User.Identity.Name, Len(InStr(User.Identity.Name, "/"))))
End Function

But it keeps underlining User...

Thank you for your time.
--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Is User a class that you made, or is that the built in user class in the framework?
 
Built in...

It's a member of
System.Web.UI.Page

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Ah, ok. We didn't use windows security, so havn't used that object before.

The simplest way to do this would be to have the function in your module take in a string, then do the parsing within it. So from your page code you'd do something like

TextBox1.Text = GetUserName(User.Identity.Name)

and in your module have

Public Function CurrentUser(userName as String) as String
CurrentUser = UCase(Right(userName, Len(InStr(userName, "/"))))
End Function

that way you never have to worry about the object being in the module.

D'Arcy
 
I was thinking about that...

Anothing thing i just thought of is to have the user name put into a session variable... what do you think of that?? So far in my project I have no session variables, and was hoping not to use them, but for this, I don't see a problem...

What's your opinion?
--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
hey Junior I think that if you simply
Import System.Web.UI.Page a the top of your module things should work. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Mark, I tried that, it didn't work...

Wish it did though...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
When you say Module do you mean class?

I made this class which sort of worked. I didn't get a username at all though with my test page I hadn't authenticated anyone. You can try it though.


Imports System.Web.UI.Page
Public Class Class1
Public Function GetUser() As String
Dim joe As New UI.Page()
Return joe.User.Identity.Name
End Function
End Class
That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
No, I'm using VS.NET and it was a function, but you did solve my problem! I needed the dim Joe and then to use joe.user!

You are the man, thanks.
--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
VS.NET shouldn't have anything to do with it.

So you were just making a function within your page?

Glad I could help though I hope that by diming a new Page object you still get the proper user data. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Junior, if its truly a module that you're using, then you don't need to Dim the joe.user object.

instead, just have a function that takes a page object by reference

Public Function GetUser(ByRef pageObject as Page) as String
'Get the user of hte page and return the user name
End Function

Otherwise, if you dim the object within the module, it may not get the right data (as Mark suggested).

D
 
Thanks... I'll try that...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
just gonna put this up for others that may have this problem.. it worked fine for me:

String userName=System.Web.HttpContext.Current.User.Identity.Name
 
I am getting empty string in System.Web.HttpContext.Current.User.Identity.Name.
(In ASP.NET web app - page load event).
Does this site have to be set to allow only Integrated Windows Authentication? (no anonymous access)
 
Yes (no anonymous access)

Rhys

""Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die."

My Home
 
Does this site have to be set to allow only Integrated Windows Authentication
Yes - The identity is based on the windows login provided. No windows login = no identity!

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Oops - cross-post with Rhys666.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top