Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Membership.FindUsersByName

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonathan Wood - 17 Dec 2007 22:48 GMT
Can anyone tell me why FindUsersByName() returns a collection? Isn't it
necessary that user names are unique (how else would passwords be
validated?)

And what is the best way to determine if a user name is already in use?
Preferrably, I could do this without raising an exception--I just want to
know whether or not they exist.

Thanks!

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

bruce barker - 18 Dec 2007 00:09 GMT
the function does a  sql like and supports sql wildcards, so even though
names are unique, the find can return multiples.

this also means to avoid injection attacks, you should check for sql
wildcard characters in usernames and email addresses.


-- bruce (sqlwork.com)

> Can anyone tell me why FindUsersByName() returns a collection? Isn't it
> necessary that user names are unique (how else would passwords be
[quoted text clipped - 5 lines]
>
> Thanks!
Jonathan Wood - 18 Dec 2007 00:22 GMT
Ah! Many thanks.

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

> the function does a  sql like and supports sql wildcards, so even though
> names are unique, the find can return multiples.
[quoted text clipped - 13 lines]
>>
>> Thanks!
Mark Fitzpatrick - 18 Dec 2007 00:09 GMT
You're looking at the wrong function. The FindUsersByName is not meant to
get a user. It's meant to find a number of users that are similar to the
username entered. The stored procedure behind this uses LIKE instead of =,
this means you could say bob% and get all usernames that begin with bob.

What you want, instead is the GetUser. Best way to see if a user exists is
to just try a call to GetUser and if it returns null, then there is no user.
If it returns a MembershipUser object that isn't null, then you have a user.

Signature

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

> Can anyone tell me why FindUsersByName() returns a collection? Isn't it
> necessary that user names are unique (how else would passwords be
[quoted text clipped - 5 lines]
>
> Thanks!
Jonathan Wood - 18 Dec 2007 00:25 GMT
Mark,

> You're looking at the wrong function. The FindUsersByName is not meant to
> get a user. It's meant to find a number of users that are similar to the
> username entered. The stored procedure behind this uses LIKE instead of =,
> this means you could say bob% and get all usernames that begin with bob.

I'm not trying to get a user, I'm trying to find the number of users with a
specific user name or, in my case, email.

> What you want, instead is the GetUser. Best way to see if a user exists is
> to just try a call to GetUser and if it returns null, then there is no
> user. If it returns a MembershipUser object that isn't null, then you have
> a user.

Here's where I get lost. Scanning the docs, I couldn't see anything about
GetUser returning null. If it does, that's perfect. But instead the
mentality seems to be raise an exception any time things aren't the way a
particular method thinks they should be.

Is there a way I'd be able to tell GetUser can return null on my own? That
would be a great help.

Thanks.

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

>> Can anyone tell me why FindUsersByName() returns a collection? Isn't it
>> necessary that user names are unique (how else would passwords be
[quoted text clipped - 5 lines]
>>
>> Thanks!
Mark Fitzpatrick - 18 Dec 2007 02:26 GMT
The documentation should always state what exceptions are thrown. Usually
when doing things such as returning objects from a function, the resultant
object will be null instead of throwing an exception. I use null tests on
GetUser(string username) all the time. Makes it much easier during a login
procedure to first check if the user even exists before bothering to
validate their credentials.

You could also use FindUsersByName but just test to see if your resultant
memberhsipusercollection has a length greater than 0 and isn't null. If it
does, then you know the user already exists. I like the GetUser though since
it's only trying to match a single name the performance may be the smallest
tad better.

Signature

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

> Mark,
>
[quoted text clipped - 31 lines]
>>>
>>> Thanks!
Jonathan Wood - 18 Dec 2007 03:41 GMT
Mark,

> The documentation should always state what exceptions are thrown. Usually
> when doing things such as returning objects from a function, the resultant
> object will be null instead of throwing an exception. I use null tests on
> GetUser(string username) all the time. Makes it much easier during a login
> procedure to first check if the user even exists before bothering to
> validate their credentials.

I agree that it would be good if GetUser() returns null instead of throwing
an error when the user does not exist. But all the docs say is GetUser
throws a NotSupportedException, but doesn't say a single thing about when it
might do so. I find the .NET docs irritatingly terse.

> You could also use FindUsersByName but just test to see if your resultant
> memberhsipusercollection has a length greater than 0 and isn't null. If it
> does, then you know the user already exists. I like the GetUser though
> since it's only trying to match a single name the performance may be the
> smallest tad better.

Unless it's returning more information. But, as long as FindUsersByName also
returns null and won't throw an exception, then that would work as well.

Thanks.

Signature

Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.