Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
123
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 2 Working with Visual Studio 2022

Managing NuGet Packages

As a software developer, being able to reuse code is essential to any modern development effort. Being able to share code is the cornerstone of a healthy development community. Many developers create handy code libraries that can add functionality to your particular application.

This is where NuGet becomes an essential tool for developers to create, share, and consume helpful code. As a developer, you can package a DLL and other required content needed for the DLL to function correctly into a NuGet package.

NuGet is just a ZIP file with a .nupkg extension containing the DLLs you have created for distribution. Included inside this package is a manifest file that contains additional information, such as the version number of the NuGet package.

Packages uploaded to nuget.org are public and available to all developers that use NuGet in their projects. However, developers can create NuGet packages that are exclusive to a particular organization and not publicly available. We will have a look at

hosting your NuGet feeds later on. For now, let’s look at how to use NuGet in your Visual Studio project.

Using NuGet in Visual Studio

Developers can access NuGet from within Visual Studio, but you can also browse www. nuget.org to find packages to use in your applications. In the following example, we will be using NuGet from within Visual Studio to add functionality to our ShipmentLocator application.

I have added a login form to the ShipmentLocator application, as seen in Figure 2-34. I want to encrypt the password typed in by the user and compare that to the encrypted password in the database.

111

Chapter 2 Working with Visual Studio 2022

Figure 2-34.  Login form added

As a rule, you should never be able to decrypt a password. If you can decrypt a password, so can others with more malicious intentions. After user registration, the encrypted password is stored in a database. Login requests are then encrypted and compared with the encrypted password in the database. If it’s a match, they are authenticated.

This is a very basic login screen but illustrates how to use NuGet in your projects. You can roll your own solution when it comes to encryption. Another route to take is

to check NuGet to see if there are any solutions available that you can use.

To add a NuGet package to your project, right-click the project in the Solution Explorer and click Manage NuGet Packages from the context menu, as seen in Figure 2-35.

112

Chapter 2 Working with Visual Studio 2022

Figure 2-35.  Manage NuGet Packages

From the NuGet Package Manager screen displayed, you can search for a NuGet package based on the keywords you enter. As seen in Figure 2-36, I will use a NuGet package called EncryptValidate that provides encryption functionality.

113

Chapter 2 Working with Visual Studio 2022

Figure 2-36.  NuGet Package Manager

The NuGet Package Manager screen provides much information about the package you are going to install. The current version number is displayed, license information, project URL, author, and download count for the particular package.

The NuGet Package Manager also makes it easy to install previous versions of the NuGet package (Figure 2-37) if you find that the latest package does not work correctly with your code.

114

Chapter 2 Working with Visual Studio 2022

Figure 2-37.  Installing previous versions

This allows you to roll back to a previous version quickly should you need to. After installing the package, the NuGet Package Manager indicates that this package has been installed, as seen in Figure 2-38.

115

Chapter 2 Working with Visual Studio 2022

Figure 2-38.  NuGet package installed

Figure 2-39 shows the NuGet package in the Visual Studio references.

Figure 2-39.  References

116

Chapter 2 Working with Visual Studio 2022

With NuGet, everything you need to use the package is added to your project. Now that we have added the EncryptValidate package to our project, let’s start adding some code as seen in Listing 2-20.

Listing 2-20.  ValidateLogin Method

private bool ValidateLogin()

{

var blnLogin = false; try

{

var password = txtPassword.Text;

// This encrypted password would be read from a database var storedEncrPassw = ReadEncryptedValueFromDatabase; if (ValidateEncryptedData(password, storedEncrPassw))

{

blnLogin = true;

}

}

catch (Exception ex)

{

_ = MessageBox.Show(ex.Message);

}

return blnLogin;

}

The encrypted password is stored in the database. It is read in and stored in the storedEncrPassw variable. The clear-text password and the stored encrypted password are then validated. If validation succeeds, the user is logged in.

Remember, the code for this project is available on GitHub at github.com/ apress/getting-started-vs2022.

By adding a single NuGet package, we have added functionality to encrypt passwords, validate encrypted passwords, and encrypt and decrypt text using Rijndael, converting text to a SecureString object, reading the value from the SecureString object, and more. All this functionality has been added without having to write the logic ourselves.

117