diff --git a/aspnetcore/security/authentication/social/google-logins.md b/aspnetcore/security/authentication/social/google-logins.md index a1ede4978a29..c52f3326cb1b 100644 --- a/aspnetcore/security/authentication/social/google-logins.md +++ b/aspnetcore/security/authentication/social/google-logins.md @@ -9,13 +9,13 @@ uid: security/authentication/google-logins --- # Google external login setup in ASP.NET Core -By [Valeriy Novytskyy](https://github.com/01binary) and [Rick Anderson](https://twitter.com/RickAndMSFT) +By [Valeriy Novytskyy](https://github.com/01binary), [Rick Anderson](https://twitter.com/RickAndMSFT) and [Sharaf Abacery](https://github.com/sharafabacery) This tutorial shows you how to enable users to sign in with their Google account using the ASP.NET Core project created on the [previous page](xref:security/authentication/social/index). ## Create the Google OAuth 2.0 Client ID and secret -* Follow the guidance in [Integrating Google Sign-In into your web app](https://developers.google.com/identity/sign-in/web/sign-in) (Google documentation). +* Follow the guidance in [Integrating Google Sign-In into your web app](https://developers.google.com/identity/gsi/web/guides/overview) (Google documentation) * Go to [Google API & Services](https://console.cloud.google.com/apis). * A **Project** must exist first, you may have to create one. Once a project is selected, enter the **Dashboard**. @@ -53,37 +53,21 @@ You can manage your API credentials and usage in the [API Console](https://conso ## Configure Google authentication -Add the [`Microsoft.AspNetCore.Authentication.Google`](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.Google) NuGet package to the app. - -:::moniker range="< aspnetcore-6.0" - -Add the Authentication service to the `Startup.ConfigureServices`: - -[!code-csharp[](~/security/authentication/social/social-code/3.x/StartupGoogle3x.cs?highlight=11-19)] - -:::moniker-end - -:::moniker range=">= aspnetcore-6.0" - -Add the Authentication service to the `Program`: - -[!code-csharp[](~/security/authentication/social/social-code/6.x/ProgramGoogle.cs)] - -:::moniker-end +* Add the [`Google.Apis.Auth.AspNetCore3`](https://www.nuget.org/packages/Google.Apis.Auth.AspNetCore3) NuGet package to the app. +* Add the Authentication service to the `program.cs`: +* Follow [`Add Authtication for asp.net app`](https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#configure-your-application-to-use-google.apis.auth.aspnetcore3) [!INCLUDE [default settings configuration](includes/default-settings2-2.md)] ## Sign in with Google +* Get a link to the libary at [google developer library link ](https://developers.google.com/identity/gsi/web/guides/client-library) to get link of library. +* Then go to [google developer button genration ](https://developers.google.com/identity/gsi/web/tools/configurator) +* Setup your Controller to match with ` data-login_uri="{HostName}/{ControllerName}/{actionName}" ` attrbute because after success login it will forward you to that link. +* Create a controller and action that takes one argument `string credential`, which is returned by Google upon completing the login process. +* Verify the `credential` using the following line of code: +`GoogleJsonWebSignature.Payload payload = await GoogleJsonWebSignature.ValidateAsync(credential);` +* This will retrieve the available information about the logged-in user, which could then be stored in a database. -* Run the app and select **Log in**. An option to sign in with Google appears. -* Select the **Google** button, which redirects to Google for authentication. -* After entering your Google credentials, you are redirected back to the web site. - -[!INCLUDE[Forward request information when behind a proxy or load balancer section](includes/forwarded-headers-middleware.md)] - -[!INCLUDE[](includes/chain-auth-providers.md)] - -For more information on configuration options supported by Google authentication, see the API reference . This can be used to request different information about the user. ## Change the default callback URI diff --git a/aspnetcore/tutorials/first-mongo-app.md b/aspnetcore/tutorials/first-mongo-app.md index 18dc4138b0d6..15c0b47656af 100644 --- a/aspnetcore/tutorials/first-mongo-app.md +++ b/aspnetcore/tutorials/first-mongo-app.md @@ -15,7 +15,7 @@ uid: tutorials/first-mongo-app By [Pratik Khandelwal](https://twitter.com/K2Prk) and [Scott Addie](https://twitter.com/Scott_Addie) -:::moniker range=">= aspnetcore-8.0" +:::moniker range=">= aspnetcore-9.0" This tutorial creates a web API that runs Create, Read, Update, and Delete (CRUD) operations on a [MongoDB](https://www.mongodb.com/what-is-mongodb) NoSQL database. @@ -35,15 +35,11 @@ In this tutorial, you learn how to: # [Visual Studio](#tab/visual-studio) -[!INCLUDE[](~/includes/net-prereqs-vs-8.0.md)] +[!INCLUDE[](~/includes/net-prereqs-vs-9.0.md)] # [Visual Studio Code](#tab/visual-studio-code) -[!INCLUDE[](~/includes/net-prereqs-vsc-8.0.md)] - -# [Visual Studio for Mac](#tab/visual-studio-mac) - -[!INCLUDE[](~/includes/net-prereqs-mac-8.0.md)] +[!INCLUDE[](~/includes/net-prereqs-vsc-9.0.md)] --- @@ -151,7 +147,11 @@ Use the previously installed MongoDB Shell in the following steps to create a da 1. Go to **File** > **New** > **Project**. 1. Select the **ASP.NET Core Web API** project type, and select **Next**. 1. Name the project *BookStoreApi*, and select **Next**. -1. Select the **.NET 8.0 (Long Term support)** framework and select **Create**. +1. In the **Additional information** dialog: + * Confirm the **Framework** is **.NET 9.0 (Standard Term Support)**. + * Confirm the checkbox for **Use controllers** is checked. + * Confirm the checkbox for **Enable OpenAPI support** is checked. + * Select **Create**. 1. In the **Package Manager Console** window, navigate to the project root. Run the following command to install the .NET driver for MongoDB: ```powershell @@ -163,7 +163,7 @@ Use the previously installed MongoDB Shell in the following steps to create a da 1. Run the following commands in a command shell: ```dotnetcli - dotnet new webapi -o BookStoreApi + dotnet new webapi -o BookStoreApi --use-controllers code BookStoreApi ``` @@ -176,17 +176,6 @@ Use the previously installed MongoDB Shell in the following steps to create a da dotnet add package MongoDB.Driver ``` -# [Visual Studio for Mac](#tab/visual-studio-mac) - -1. Select **File** > **New Project...**. -1. Select **Web and Console** > **App** from the sidebar. -1. Select the **ASP.NET Core** > **API** C# project template, and select **Next**. -1. Select **.NET 8.0** from the **Target Framework** drop-down list, and select **Next**. -1. Enter *BookStoreApi* for the **Project Name**, and select **Create**. -1. In the **Solution** pad, right-click the project's **Dependencies** node and select **Manage NuGet Packages**. -1. Enter *MongoDB.Driver* in the search box, select the *MongoDB.Driver* package, and select **Add Package**. -1. Select the **Accept** button in the **License Acceptance** dialog. - --- ## Add an entity model @@ -352,6 +341,8 @@ To satisfy the preceding requirements, make the following changes: :::moniker-end +[!INCLUDE[](~/tutorials/first-mongo-app/includes/first-mongo-app8.md)] + [!INCLUDE[](~/tutorials/first-mongo-app/includes/first-mongo-app7.md)] [!INCLUDE[](~/tutorials/first-mongo-app/includes/first-mongo-app6.md)] diff --git a/aspnetcore/tutorials/first-mongo-app/includes/first-mongo-app8.md b/aspnetcore/tutorials/first-mongo-app/includes/first-mongo-app8.md new file mode 100644 index 000000000000..ff275e3a87be --- /dev/null +++ b/aspnetcore/tutorials/first-mongo-app/includes/first-mongo-app8.md @@ -0,0 +1,336 @@ +:::moniker range="= aspnetcore-8.0" + +This tutorial creates a web API that runs Create, Read, Update, and Delete (CRUD) operations on a [MongoDB](https://www.mongodb.com/what-is-mongodb) NoSQL database. + +In this tutorial, you learn how to: + +> [!div class="checklist"] +> * Configure MongoDB +> * Create a MongoDB database +> * Define a MongoDB collection and schema +> * Perform MongoDB CRUD operations from a web API +> * Customize JSON serialization + +## Prerequisites + +* [MongoDB 6.0.5 or later](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/) +* [MongoDB Shell](https://www.mongodb.com/docs/mongodb-shell/install/) + +# [Visual Studio](#tab/visual-studio) + +[!INCLUDE[](~/includes/net-prereqs-vs-8.0.md)] + +# [Visual Studio Code](#tab/visual-studio-code) + +[!INCLUDE[](~/includes/net-prereqs-vsc-8.0.md)] + +# [Visual Studio for Mac](#tab/visual-studio-mac) + +[!INCLUDE[](~/includes/net-prereqs-mac-8.0.md)] + +--- + +## Configure MongoDB + + +Enable MongoDB and MongoDB Shell access from anywhere on the development machine (Windows/Linux/macOS): + +1. Download and Install MongoDB Shell: + * macOS/Linux: Choose a directory to extract the MongoDB Shell to. Add the resulting path for `mongosh` to the `PATH` environment variable. + * Windows: MongoDB Shell (mongosh.exe) is installed at *C:\Users\\AppData\Local\Programs\mongosh*. Add the resulting path for `mongosh.exe` to the `PATH` environment variable. +1. Download and Install MongoDB: + * macOS/Linux: Verify the directory that MongoDB was installed at, usually in */usr/local/mongodb*. Add the resulting path for `mongodb` to the `PATH` environment variable. + * Windows: MongoDB is installed at *C:\\Program Files\MongoDB* by default. Add *C:\\Program Files\\MongoDB\\Server\\\\\bin* to the `PATH` environment variable. +1. Choose a Data Storage Directory: Select a directory on your development machine for storing data. Create the directory if it doesn't exist. The MongoDB Shell doesn't create new directories: + * macOS/Linux: For example, `/usr/local/var/mongodb`. + * Windows: For example, `C:\\BooksData`. +1. In the OS command shell (not the MongoDB Shell), use the following command to connect to MongoDB on default port 27017. Replace `` with the directory chosen in the previous step. + + ```console + mongod --dbpath + ``` + +Use the previously installed MongoDB Shell in the following steps to create a database, make collections, and store documents. For more information on MongoDB Shell commands, see [`mongosh`](https://docs.mongodb.com/mongodb-shell/run-commands/). + +1. Open a MongoDB command shell instance by launching `mongosh.exe`. +1. In the command shell, connect to the default test database by running the following command: + + ```console + mongosh + ``` + +1. Run the following command in the command shell: + + ```console + use BookStore + ``` + + A database named *BookStore* is created if it doesn't already exist. If the database does exist, its connection is opened for transactions. + +1. Create a `Books` collection using following command: + + ```console + db.createCollection('Books') + ``` + + The following result is displayed: + + ```console + { "ok" : 1 } + ``` + +1. Define a schema for the `Books` collection and insert two documents using the following command: + + ```console + db.Books.insertMany([{ "Name": "Design Patterns", "Price": 54.93, "Category": "Computers", "Author": "Ralph Johnson" }, { "Name": "Clean Code", "Price": 43.15, "Category": "Computers","Author": "Robert C. Martin" }]) + ``` + + A result similar to the following is displayed: + + ```console + { + "acknowledged" : true, + "insertedIds" : [ + ObjectId("61a6058e6c43f32854e51f51"), + ObjectId("61a6058e6c43f32854e51f52") + ] + } + ``` + + > [!NOTE] + > The `ObjectId`s shown in the preceding result won't match those shown in the command shell. + +1. View the documents in the database using the following command: + + ```console + db.Books.find().pretty() + ``` + + A result similar to the following is displayed: + + ```console + { + "_id" : ObjectId("61a6058e6c43f32854e51f51"), + "Name" : "Design Patterns", + "Price" : 54.93, + "Category" : "Computers", + "Author" : "Ralph Johnson" + } + { + "_id" : ObjectId("61a6058e6c43f32854e51f52"), + "Name" : "Clean Code", + "Price" : 43.15, + "Category" : "Computers", + "Author" : "Robert C. Martin" + } + ``` + + The schema adds an autogenerated `_id` property of type `ObjectId` for each document. + +## Create the ASP.NET Core web API project + +# [Visual Studio](#tab/visual-studio) + +1. Go to **File** > **New** > **Project**. +1. Select the **ASP.NET Core Web API** project type, and select **Next**. +1. Name the project *BookStoreApi*, and select **Next**. +1. Select the **.NET 8.0 (Long Term support)** framework and select **Create**. +1. In the **Package Manager Console** window, navigate to the project root. Run the following command to install the .NET driver for MongoDB: + + ```powershell + Install-Package MongoDB.Driver + ``` + +# [Visual Studio Code](#tab/visual-studio-code) + +1. Run the following commands in a command shell: + + ```dotnetcli + dotnet new webapi -o BookStoreApi + code BookStoreApi + ``` + + The preceding commands generate a new ASP.NET Core web API project and then open the project in Visual Studio Code. + +1. Once the OmniSharp server starts up, a dialog asks **Required assets to build and debug are missing from 'BookStoreApi'. Add them?**. Select **Yes**. +1. Open the **Integrated Terminal** and run the following command to install the .NET driver for MongoDB: + + ```dotnetcli + dotnet add package MongoDB.Driver + ``` + +# [Visual Studio for Mac](#tab/visual-studio-mac) + +1. Select **File** > **New Project...**. +1. Select **Web and Console** > **App** from the sidebar. +1. Select the **ASP.NET Core** > **API** C# project template, and select **Next**. +1. Select **.NET 8.0** from the **Target Framework** drop-down list, and select **Next**. +1. Enter *BookStoreApi* for the **Project Name**, and select **Create**. +1. In the **Solution** pad, right-click the project's **Dependencies** node and select **Manage NuGet Packages**. +1. Enter *MongoDB.Driver* in the search box, select the *MongoDB.Driver* package, and select **Add Package**. +1. Select the **Accept** button in the **License Acceptance** dialog. + +--- + +## Add an entity model + +1. Add a *Models* directory to the project root. +1. Add a `Book` class to the *Models* directory with the following code: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples_snapshot/6.x/Book.cs"::: + + In the preceding class, the `Id` property is: + + * Required for mapping the Common Language Runtime (CLR) object to the MongoDB collection. + * Annotated with [`[BsonId]`](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/T_MongoDB_Bson_Serialization_Attributes_BsonIdAttribute.htm) to make this property the document's primary key. + * Annotated with [`[BsonRepresentation(BsonType.ObjectId)]`](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/T_MongoDB_Bson_Serialization_Attributes_BsonRepresentationAttribute.htm) to allow passing the parameter as type `string` instead of an [ObjectId](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/T_MongoDB_Bson_ObjectId.htm) structure. Mongo handles the conversion from `string` to `ObjectId`. + + The `BookName` property is annotated with the [`[BsonElement]`](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/T_MongoDB_Bson_Serialization_Attributes_BsonElementAttribute.htm) attribute. The attribute's value of `Name` represents the property name in the MongoDB collection. + +## Add a configuration model + +1. Add the following database configuration values to `appsettings.json`: + + :::code language="json" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/appsettings.json" highlight="2-6"::: + +1. Add a `BookStoreDatabaseSettings` class to the *Models* directory with the following code: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Models/BookStoreDatabaseSettings.cs"::: + + The preceding `BookStoreDatabaseSettings` class is used to store the `appsettings.json` file's `BookStoreDatabase` property values. The JSON and C# property names are named identically to ease the mapping process. + +1. Add the following highlighted code to `Program.cs`: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Program.cs" id="snippet_BookStoreDatabaseSettings" highlight="4-5"::: + + In the preceding code, the configuration instance to which the `appsettings.json` file's `BookStoreDatabase` section binds is registered in the Dependency Injection (DI) container. For example, the `BookStoreDatabaseSettings` object's `ConnectionString` property is populated with the `BookStoreDatabase:ConnectionString` property in `appsettings.json`. + +1. Add the following code to the top of `Program.cs` to resolve the `BookStoreDatabaseSettings` reference: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Program.cs" id="snippet_UsingModels"::: + +## Add a CRUD operations service + +1. Add a *Services* directory to the project root. +1. Add a `BooksService` class to the *Services* directory with the following code: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Services/BooksService.cs" id="snippet_File"::: + + In the preceding code, a `BookStoreDatabaseSettings` instance is retrieved from DI via constructor injection. This technique provides access to the `appsettings.json` configuration values that were added in the [Add a configuration model](#add-a-configuration-model) section. + +1. Add the following highlighted code to `Program.cs`: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Program.cs" id="snippet_BooksService" highlight="7"::: + + In the preceding code, the `BooksService` class is registered with DI to support constructor injection in consuming classes. The singleton service lifetime is most appropriate because `BooksService` takes a direct dependency on `MongoClient`. Per the official [Mongo Client reuse guidelines](https://mongodb.github.io/mongo-csharp-driver/2.14/reference/driver/connecting/#re-use), `MongoClient` should be registered in DI with a singleton service lifetime. + +1. Add the following code to the top of `Program.cs` to resolve the `BooksService` reference: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Program.cs" id="snippet_UsingServices"::: + +The `BooksService` class uses the following `MongoDB.Driver` members to run CRUD operations against the database: + +* [MongoClient](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/T_MongoDB_Driver_MongoClient.htm): Reads the server instance for running database operations. The constructor of this class is provided in the MongoDB connection string: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Services/BooksService.cs" id="snippet_ctor" highlight="4-5"::: + +* [IMongoDatabase](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/T_MongoDB_Driver_IMongoDatabase.htm): Represents the Mongo database for running operations. This tutorial uses the generic [GetCollection\(collection)](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/M_MongoDB_Driver_IMongoDatabase_GetCollection__1.htm) method on the interface to gain access to data in a specific collection. Run CRUD operations against the collection after this method is called. In the `GetCollection(collection)` method call: + + * `collection` represents the collection name. + * `TDocument` represents the CLR object type stored in the collection. + +`GetCollection(collection)` returns a [MongoCollection](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/T_MongoDB_Driver_MongoCollection.htm) object representing the collection. In this tutorial, the following methods are invoked on the collection: + +* [DeleteOneAsync](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/M_MongoDB_Driver_IMongoCollection_1_DeleteOneAsync_1.htm): Deletes a single document matching the provided search criteria. +* [Find\](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/M_MongoDB_Driver_IMongoCollectionExtensions_Find__1.htm): Returns all documents in the collection matching the provided search criteria. +* [InsertOneAsync](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/M_MongoDB_Driver_IMongoCollection_1_InsertOneAsync_1.htm): Inserts the provided object as a new document in the collection. +* [ReplaceOneAsync](https://mongodb.github.io/mongo-csharp-driver/2.14/apidocs/html/M_MongoDB_Driver_IMongoCollection_1_ReplaceOneAsync.htm): Replaces the single document matching the provided search criteria with the provided object. + +## Add a controller + +Add a `BooksController` class to the *Controllers* directory with the following code: + +:::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Controllers/BooksController.cs"::: + +The preceding web API controller: + +* Uses the `BooksService` class to run CRUD operations. +* Contains action methods to support GET, POST, PUT, and DELETE HTTP requests. +* Calls in the `Create` action method to return an [HTTP 201](https://www.rfc-editor.org/rfc/rfc9110#status.201) response. Status code 201 is the standard response for an HTTP POST method that creates a new resource on the server. `CreatedAtAction` also adds a `Location` header to the response. The `Location` header specifies the URI of the newly created book. + +## Test the web API + +1. Build and run the app. + +1. Navigate to `https://localhost:/api/books`, where `` is the automatically assigned port number for the app, to test the controller's parameterless `Get` action method, select **Try it out** > **Execute**. A JSON response similar to the following is displayed: + + ```json + [ + { + "id": "61a6058e6c43f32854e51f51", + "bookName": "Design Patterns", + "price": 54.93, + "category": "Computers", + "author": "Ralph Johnson" + }, + { + "id": "61a6058e6c43f32854e51f52", + "bookName": "Clean Code", + "price": 43.15, + "category": "Computers", + "author": "Robert C. Martin" + } + ] + ``` + +1. Navigate to `https://localhost:/api/books/{id here}` to test the controller's overloaded `Get` action method. A JSON response similar to the following is displayed: + + ```json + { + "id": "61a6058e6c43f32854e51f52", + "bookName": "Clean Code", + "price": 43.15, + "category": "Computers", + "author": "Robert C. Martin" + } + ``` + +## Configure JSON serialization options + +There are two details to change about the JSON responses returned in the [Test the web API](#test-the-web-api) section: + +* The property names' default camel casing should be changed to match the Pascal casing of the CLR object's property names. +* The `bookName` property should be returned as `Name`. + +To satisfy the preceding requirements, make the following changes: + +1. In `Program.cs`, chain the following highlighted code on to the `AddControllers` method call: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Program.cs" id="snippet_AddControllers" highlight="10-11"::: + + With the preceding change, property names in the web API's serialized JSON response match their corresponding property names in the CLR object type. For example, the `Book` class's `Author` property serializes as `Author` instead of `author`. + +1. In `Models/Book.cs`, annotate the `BookName` property with the [`[JsonPropertyName]`](xref:System.Text.Json.Serialization.JsonPropertyNameAttribute) attribute: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Models/Book.cs" id="snippet_BookName" highlight="2"::: + + The `[JsonPropertyName]` attribute's value of `Name` represents the property name in the web API's serialized JSON response. + +1. Add the following code to the top of `Models/Book.cs` to resolve the `[JsonProperty]` attribute reference: + + :::code language="csharp" source="~/tutorials/first-mongo-app/samples/6.x/BookStoreApi/Models/Book.cs" id="snippet_UsingSystemTextJsonSerialization"::: + +1. Repeat the steps defined in the [Test the web API](#test-the-web-api) section. Notice the difference in JSON property names. + +## Add authentication support to a web API + +[!INCLUDE[](~/includes/DuendeIdentityServer.md)] + +## Additional resources + +* [View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/tutorials/first-mongo-app/samples) ([how to download](xref:index#how-to-download-a-sample)) +* +* +* [Create a web API with ASP.NET Core](/training/modules/build-web-api-aspnet-core/) + +:::moniker-end