0% found this document useful (0 votes)
61 views4 pages

Sample API .Net Core

This document outlines the steps to create an ASP.NET Core Web API application with Azure Active Directory authentication in Visual Studio 2019. Key steps include: 1. Installing the Azure AD authentication NuGet package. 2. Configuring the API to use client ID and tenant ID from app registrations in the Azure Active Directory. 3. Registering authentication and CORS services in the Startup class to enable authentication and allow requests from Angular application. 4. Creating sample Employee and EmployeesController classes to return test data from API upon authenticated request.

Uploaded by

Digil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
61 views4 pages

Sample API .Net Core

This document outlines the steps to create an ASP.NET Core Web API application with Azure Active Directory authentication in Visual Studio 2019. Key steps include: 1. Installing the Azure AD authentication NuGet package. 2. Configuring the API to use client ID and tenant ID from app registrations in the Azure Active Directory. 3. Registering authentication and CORS services in the Startup class to enable authentication and allow requests from Angular application. 4. Creating sample Employee and EmployeesController classes to return test data from API upon authenticated request.

Uploaded by

Digil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Create ASP.

NET Core Web API in Visual Studio 2019


We can create ASP.NET Core Web API application using default API template in
Visual Studio.

We must install “Microsoft.AspNetCore.Authentication.AzureAD.UI” library using


NuGet. This is used for AD authentication.

We have already created two app registrations in Azure active directory. We can
use the client id and tenant id for API here in appsettings as given below.

appsettings.json

1. {
2. "Logging": {
3. "LogLevel": {
4. "Default": "Information",
5. "Microsoft": "Warning",
6. "Microsoft.Hosting.Lifetime": "Information"
7. }
8. },
9. "AllowedHosts": "*",
10. "AzureActiveDirectory": {
11. "Instance": "https://login.microsoftonline.com/",
12. "Domain": "<your domain>.onmicrosoft.com",
13. "TenantId": "adbbbd82-76e5-4952-8531-3cc59f3c1fdd",
14. "ClientId": "api://e283d8fb-22ad-4e2c-9541-
14f6f118a08f"
15. }
16. }

We can register authentication service inside the ConfigureServices method in


Startup class. Also add CORS service as well.

Staturp.cs

1. using Microsoft.AspNetCore.Authentication;
2. using Microsoft.AspNetCore.Authentication.AzureAD.UI;
3. using Microsoft.AspNetCore.Builder;
4. using Microsoft.AspNetCore.Hosting;
5. using Microsoft.Extensions.Configuration;
6. using Microsoft.Extensions.DependencyInjection;
7. using Microsoft.Extensions.Hosting;
8. using System;
9.
10. namespace AzureADAPI
11. {
12. public class Startup
13. {
14. public Startup(IConfiguration configuration)
15. {
16. Configuration = configuration;
17. }
18.
19. public IConfiguration Configuration { get; }
20.
21. // This method gets called by the runtime. Use th
is method to add services to the container.
22. public void ConfigureServices(IServiceCollection
services)
23. {
24. services.AddControllers();
25.
26. services.AddAuthentication(AzureADDefaults.Be
arerAuthenticationScheme).AddAzureADBearer(options => Configur
ation.Bind("AzureActiveDirectory", options));
27.
28. string corsDomains = "http://localhost:4200";

29. string[] domains = corsDomains.Split(",".ToCh


arArray(), StringSplitOptions.RemoveEmptyEntries);
30.
31. services.AddCors(o => o.AddPolicy("AppCORSPol
icy", builder =>
32. {
33. builder.AllowAnyOrigin()
34. .AllowAnyMethod()
35. .AllowAnyHeader()
36. .AllowCredentials()
37. .WithOrigins(domains);
38. }));
39.
40. }
41.
42. // This method gets called by the runtime. Use th
is method to configure the HTTP request pipeline.
43. public void Configure(IApplicationBuilder app, IW
ebHostEnvironment env)
44. {
45. if (env.IsDevelopment())
46. {
47. app.UseDeveloperExceptionPage();
48. }
49.
50. app.UseCors("AppCORSPolicy");
51.
52. app.UseRouting();
53.
54. app.UseAuthentication();
55. app.UseAuthorization();
56.
57. app.UseEndpoints(endpoints =>
58. {
59. endpoints.MapControllers();
60. });
61. }
62. }
63. }

Create an Employee class. This will be used in our Employees controller class to
return some dummy data to Angular application later.

Employee.cs

1. namespace AzureADAPI
2. {
3. public class Employee
4. {
5. public int Id { get; set; }
6. public string Name { get; set; }
7. public string Company { get; set; }
8. public string City { get; set; }
9. }
10. }

Create Employee controller with single Get method. This method will be called
from Angular application to test AD authentication.

EmployeeController.cs

1. using Microsoft.AspNetCore.Authorization;
2. using Microsoft.AspNetCore.Mvc;
3. using System.Collections.Generic;
4.
5. // For more information on enabling Web API for empty projects
, visit https://go.microsoft.com/fwlink/?LinkID=397860
6.
7. namespace AzureADAPI.Controllers
8. {
9. [Authorize]
10. [Route("api/[controller]")]
11. public class EmployeesController : Controller
12. {
13. [HttpGet]
14. public IEnumerable<Employee> Get()
15. {
16. List<Employee> employees = new List<Employee>

17. {
18. new Employee { Id = 1, Name = "Sarathlal
Saseendran", Company = "Orion Business Innovations", City = "K
ochi" },
19. new Employee { Id = 2, Name = "Anil Soman
", Company = "Cognizant", City = "Bangalare" }
20. };
21. return employees;
22. }
23. }
24. }

Please note, we have decorated above controller with [Authorize] decorator.

We have completed the API application enabled with AD authentication. We can


create Angular application from scratch and add all the components and
services.

You might also like