3

I am trying to write a simple Gui to produce a random password. The code I am trying to use is:

using System.Web.Security;

Password_txtBx.Text = Membership.GeneratePassword(12, 1);

I am getting the error: "The name 'Membership' does not exist in the current context"

I have changed the code to this:

Password_txtBx.Text = System.Web.Security.Membership.GeneratePassword(12, 1);

The error message I get then is: "The type or namespace 'SecurityMembership' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)"

I have referenced System.Web.Security in both examples above. Is there any way to fix my problem?

ajaustin12
  • 111
  • 1
  • 3
  • 10
  • Are you using .NET 1.x? The Membership class was introduced in .NET 2.0. http://msdn.microsoft.com/en-us/library/system.web.security.membership.aspx – Lance U. Matthews Mar 10 '12 at 05:26

3 Answers3

7

Add a reference to System.Web (References -> RightClick -> AddReference -> .NET - > System.Web)

Now add a using (or Imports if using VB) for System.Web.Security

You may need to change Profile to .Net (instead of .Net Client Profile)

LenPopLilly
  • 1,227
  • 3
  • 13
  • 21
  • Thank you. I figured out how to change "You may need to change Profile to .Net (instead of .Net Client Profile)" and got it to work. – ajaustin12 Mar 10 '12 at 05:48
4

Are you sure that is the exact code/error message?

The error message you posted:

"The type or namespace 'SecurityMembership' does not exist in the namespace 'System.Web' (are you missing an assembly reference?"

implies you are trying to access SecurityMembership class. There is no such thing. You should add a dot after Security. Also, the class is defined in System.Web.dll, not System.Web.Security.dll. Note that System.Web assembly is not available in .NET Framework Client Profile. To be able to use that, you need to target full .NET Framework. You can change the target profile of the project in Project Properties.

There are other ways to generate random passwords that do not require you to add a dependency on System.Web.dll assembly (since you are doing this in a Windows Forms environment). Additionally, you can use System.Security.Cryptography classes to generate cryptographically secure random numbers.

Community
  • 1
  • 1
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • I fixed the missing dot. It now gives me an error saying "The type or namespace 'Membership' does not exist in the namespace 'System.Web.Security' (are you missing an assembly reference?" – ajaustin12 Mar 10 '12 at 05:26
  • @ajaustin12 Are you sure you have added a reference to **System.Web** assembly? System.Web, not System.Web.Security. Check that in solution explorer under "References". If you cannot see it there, right click on References, click "Add Reference" and select System.Web. – Mehrdad Afshari Mar 10 '12 at 05:28
  • When I look up the .Net references I only have the following: System.Web.Services and System.Web.ApplicationServices I have selected both and no change. – ajaustin12 Mar 10 '12 at 05:28
  • @ajaustin12 Which .NET Framework profile are you targeting? .NET Client Profile probably does not have System.Web. – Mehrdad Afshari Mar 10 '12 at 05:29
  • I am lost now. I am new to C# all I know is I am using MSVS 2010 Proffesional. At the top of the add reference screen it says .NET Framework 4 Client Profile – ajaustin12 Mar 10 '12 at 05:31
  • 1
    @ajaustin12 Yeah, I guessed that. You should either switch to .NET Framework full profile in project properties, which prevents your application from running with only client profile installed and requires full .NET Framework, or you cannot use the Membership class in System.Web, as it is not available in client profile. As I said, you can rely on other methods to generate random passwords linked in the answer. – Mehrdad Afshari Mar 10 '12 at 05:33
  • Thank you for you help I guess I am to new at C# to figure this out. – ajaustin12 Mar 10 '12 at 05:36
-1

Add using System.Web.Security; namespace in .cs file.

Jason
  • 11,744
  • 3
  • 42
  • 46