The name Membership does not exist in current context - c#

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?

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)

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.

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

Related

Acumatica - error when customizing "Marketing Campaign" screen

I am having the following error when adding any custom field or overriding any attribute on existing fields on Marketing Campaign screen:
\App_RuntimeCode\PX_Objects_CR_CRCampaign_extensions.cs(1): error CS0246: The type or namespace name 'AP' could not be found (are you missing a using directive or an assembly reference?)
Any idea on where to track this error?
This likely occurred because of using static directive or nested namespace.
For example, having AP.xyz nested in PX.Objects.AP produces no compiler error:
When AP.xyz is nested in another namespace it's not found:
The issue with using static directive is a bit different but results in the same error. This can occur because runtime code uses IIS for compilation and IIS compiler is not the latest dot net compiler. So using static directive can work when you compile a dynamic library extension in visual studio but can fail when it's compiled by IIS as part of a runtime customization.
The easy fix is to edit the files to either:
Fully qualify the identifiers by changing AP.xyz to PX.Objects.AP.xyz
Sometimes all that is required is to remove AP. and add using PX.Objects.AP at the top of the file if it's not there. When there's a conflict between 2 types with the same name in different namespace you must use method #1.

RandomNumberGenerator in ASP.NET5

I have created a new Web API project using the ASP.NET 5 Templates. I am wanting to generate random numbers using System.Security.Cryptography.RandomNumberGenerator but it is not available in the .NET Platform 5.4 (see screenshot).
Compiling it also generates the following errors:
Error CS0234 The type or namespace name 'Cryptography' does not exist in the namespace 'System.Security' (are you missing an assembly reference?)
Error CS0246 The type or namespace name 'RandomNumberGenerator' could not be found (are you missing a using directive or an assembly reference?)
Error CS0103 The name 'RandomNumberGenerator' does not exist in the current context
Is there a way to use this or is there an alternative random generator that I can use which is cryptographically secure?
I got it working thanks to Scott Chamberlain's comment by adding the System.Security.Cryptography.Algorithms NuGet package.
I also had to modify the project.json so it was only a dependency of dotnet5.4 and not a global dependency otherwise the compiler would complain that RandomNumberGenerator exists in both System.Security.Cryptography.Algorithms and mscorlib.

Error : does not exist in the namespace 'Common' when i installed Quartz.Net

I have a .Net 4.0 project, which has a class named Common to do somethings like getQueryString, GetKey, etc.
When I installed the latest Quartz.Net for my Project, I started getting error messages that I'm missing namespace Common in my project.
Here's the actual error message:
error CS0234: The type or namespace name 'QueryString' does not exist
in the namespace 'Common' (are you missing an assembly reference?)
Quartz probably has a namespace or subnamespace called Common and now you have a conflict.
You can try to find every problematic instance and either remove the Quartz using OR qualify your class name fully (that means using the full name including it's namespace).
Or you could remove Quartz, rename your own class to something more fitting and non-generic than "Common" and then install Quartz again. The removal and reinstallation of Quartz will make it easier to rename your class using the rename class feature of Visual Studio.

Reference to SQL Database Project (SQLCLR) Throws "type or namespace could not be found" Error

I have spent literally a 1 1/2 days Googling for a solution to this issue to no avail.
I am unable to reference a compiled SQLCLR assembly from code, in another project. I can add the reference to the other project, but when I try to add a "using" directive to include the referenced component in code, I receive a "The type or namespace name 'Database1' could not be found (are you missing a using directive or an assembly reference?)"
Here's what I've done:
Using VS 2012/2013 I have created a blank solution.
Added a new SQL Server Database project (Database1) targeting SQL Server 2012, and the .Net 4.5 Framework (NOT the Client Profile).
Added a new command line/winform/web app/whatever project (TestConsoleApplication1) to the solution, that ALSO targets the .Net 4.5 Framework (NOT the Client Profile).
Added references to the Database1 assembly (have tried project reference as well), and the System.Data assembly to the TestConsoleApplication1 project.
Added using directive for the System.Data assembly to the default class which was automatically added when I created the TestConsoleApplication1 project.
Attempted to add a using directive for the Database1 assembly to the default class which was automatically added when I created the TestConsoleApplication1 project.
When attempting to add the using directive for the Database1 assembly, the assembly name did not appear in intellisense, and typing in the assembly name resulted in the "red squiggle" under Database1.
Attempted to build the project, results in the "The type or namespace name 'Database1' could not be found (are you missing a using directive or an assembly reference?)" Error.
Checked, double, triple, quadruple...n-tuple-checked that both projects target the same .Net Framework version (4.5).
Database1 has no code, and here is the code for the "client" app (TestConsoleApplication1):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Database1; //Marked as design-time error, and throws ERROR on build
namespace TestConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}
Does anybody have any idea why I can add a reference to the SQLCLR assembly, but the "client" project code appears unable to actually "see" nor access the referenced assembly?
Your .net console project can not reference the .Net assembly that is within the SQLCLR project as it will be embedded in the database when you publish it. It does combile to a DLL but would be dependant on the Microsoft Sql Server libs and would normally run under the same connection context vs a defined connection string to talk back to the database. Both projects could reference a third, shared assembly, then add SQLCLR .net code directly in the database project that simply calls the shared code as a wrapper.

Unable to move ASP.NET Identity model into another assembly

I have tried to follow the steps described in this link in order to pull out my identity class from the web application into its own class library.
http://www.umbraworks.net/bl0g/rebuildall/2013/10/22/Moving_ASP_NET_Identity_model_into_another_assembly
I do as follows:
Created a web form project and named it 3TierExample.
Created new class class library called 3TierExample.Model.
Installed Microsoft ASP.NET Identity EntityFramework package from NuGet into 3TierExample.Model
Moved identityModel.cs to the new class library (3TierExample.Model)
Added a reference to 3TierExample.Model in 3TierExample
Then for the identityModel.cs I use the following references:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using System.Web;
using System;
using _3TierExample.Models;
My namespace is as follows:
namespace _3TierExample.Models{…}
However when I finish I get the following errors:
I:..\3TierExample\3TierExample\Default.aspx: ASP.NET runtime error: Could not load file or assembly 'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040)
I:...\3TierExample\3TierExample\Default.aspx 1 1 3TierExample
Metadata file 'I:..\3TierExample\3TierExample.Model\bin\Debug\3TierExample.Model.dll' could not be found
The type or namespace name 'HttpRequest' could not be found (are you missing a using directive or an assembly reference?) I:...\3TierExample
The type or namespace name 'HttpResponse' could not be found (are you missing a using directive or an assembly reference?) I:...\3TierExample
If I go to the web project I see that the reference to the newly created class library is there, so I don’t know how is it possible to get the first error (No 3). Then, the second error happens as a consequence, I think, of not finding the assembly reference. Why? The reference is there!?
Can anybody help?
Thank you in advance!!
There is an assembly conflicts.
When you create project, it could be having old version Asp.net Identity assemblies. You installed latest Asp.net Identity Packages in your class lib proj. If you want to separate Asp.net Identity Context from the main project, you don't need that framework in your web proj.
you can set No Authentication while creating your web project, which make sure there will not be conflicts when reference to Asp.net Identity Class Lib Project. You may require to copy IdentityModels.cs from sample Web Project in order to use in Class Lib Proj.

Categories