Skip to content

Commit c7e7914

Browse files
author
ajeet kumar
committed
first commit
1 parent b5c332f commit c7e7914

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2119
-3
lines changed

.dockerignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.dockerignore
2+
.env
3+
.git
4+
.gitignore
5+
.vs
6+
.vscode
7+
docker-compose.yml
8+
docker-compose.*.yml
9+
*/bin
10+
*/obj

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ BenchmarkDotNet.Artifacts/
5252
project.lock.json
5353
project.fragment.lock.json
5454
artifacts/
55-
**/Properties/launchSettings.json
5655

5756
# StyleCop
5857
StyleCopReport.xml
@@ -328,3 +327,4 @@ ASALocalRun/
328327

329328
# MFractors (Xamarin productivity tool) working folder
330329
.mfractor/
330+
/gateway.api/configuration.Development.json

AuthorizeService/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin\
2+
obj\
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
using Ocelot.JwtAuthorize;
8+
using System.Security.Claims;
9+
using Authorize.Model;
10+
using Microsoft.AspNetCore.Http;
11+
using AuthorizeService.Service;
12+
using AutoMapper;
13+
14+
namespace Authorize.Controllers
15+
{
16+
/// <summary>
17+
/// Authorize
18+
/// </summary>
19+
[Route("authorize-microservice/[controller]")]
20+
[ApiController]
21+
public class CustomersController : Controller
22+
{
23+
readonly ILogger<CustomersController> _logger;
24+
readonly ITokenBuilder _tokenBuilder;
25+
ICustomerService _customerService;
26+
IMapper _mapper;
27+
/// <summary>
28+
///
29+
/// </summary>
30+
/// <param name="tokenBuilder"></param>
31+
/// <param name="logger"></param>
32+
/// <param name="mapper"></param>
33+
/// <param name="customerService"></param>
34+
public CustomersController(ITokenBuilder tokenBuilder, ILogger<CustomersController> logger,ICustomerService customerService,IMapper mapper)
35+
{
36+
_logger = logger;
37+
_tokenBuilder = tokenBuilder;
38+
_customerService=customerService;
39+
_mapper = mapper;
40+
}
41+
/// <summary>
42+
/// Get all customers
43+
/// </summary>
44+
/// <returns>returns all customers</returns>
45+
[HttpGet(Name = "get-customers")]
46+
[ProducesResponseType(StatusCodes.Status200OK)]
47+
[ProducesResponseType(StatusCodes.Status404NotFound)]
48+
public ActionResult<IEnumerable<CustomerModel>> Get()
49+
{
50+
_logger.LogInformation($"getting customers ...");
51+
var customers = _customerService.GetCustomers();
52+
if (customers == null) NotFound();
53+
var customersModel = _mapper.Map<IEnumerable<CustomerModel>>(customers);
54+
return Ok(new { Customers = customersModel });
55+
}
56+
/// <summary>
57+
/// Customer login
58+
/// </summary>
59+
/// <param name="loginModel">Model</param>
60+
/// <returns>login customer</returns>
61+
[ProducesResponseType( StatusCodes.Status200OK)]
62+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
63+
[ProducesResponseType(StatusCodes.Status404NotFound)]
64+
[HttpPost("login")]
65+
public ActionResult Login([FromBody]LoginModel loginModel)
66+
{
67+
// No model state validation code here in dotnet ore 2.1, hooray!
68+
string message = string.Empty;
69+
var login = _mapper.Map<Login>(loginModel);
70+
var userIsValid=_customerService.Login(login);
71+
message = $"{loginModel.UserName} login!";
72+
_logger.LogInformation(message);
73+
if (userIsValid)
74+
{
75+
var claims = new Claim[] {
76+
new Claim(ClaimTypes.Name, loginModel.UserName),
77+
new Claim(ClaimTypes.Role, "admin"),
78+
79+
};
80+
var token = _tokenBuilder.BuildJwtToken(claims);
81+
message = $"{loginModel.UserName} login success,and generate token return";
82+
_logger.LogInformation(message);
83+
return Ok(token);
84+
}
85+
else
86+
{
87+
message = $"{loginModel.UserName} login fails";
88+
_logger.LogInformation(message);
89+
return BadRequest(message);
90+
}
91+
}
92+
/// <summary>
93+
/// Customer register
94+
/// </summary>
95+
/// <param name="customerModel">Model</param>
96+
/// <returns>return regiter message</returns>
97+
[ProducesResponseType(StatusCodes.Status201Created)]
98+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
99+
[HttpPost("create")]
100+
public ActionResult Register([FromBody]CustomerModel customerModel)
101+
{
102+
// No model state validation code here in dotnet core 2.1, hooray!
103+
104+
_logger.LogInformation($"{customerModel.UserName} register!");
105+
var customer = _mapper.Map<Customer>(customerModel);
106+
107+
var createdCustomer=_customerService.AddCustomer(customer);
108+
if (createdCustomer != null)
109+
{
110+
_logger.LogInformation($"{customer.UserName} register success");
111+
return CreatedAtAction("login", new { id = createdCustomer.ID}, createdCustomer);
112+
}
113+
else
114+
{
115+
string message = $"{customer.UserName} register failes";
116+
_logger.LogInformation(message);
117+
return BadRequest(message);
118+
}
119+
}
120+
/// <summary>
121+
/// Delete a customer
122+
/// </summary>
123+
/// <param name="userName"></param>
124+
[ProducesResponseType(StatusCodes.Status200OK)]
125+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
126+
[ProducesResponseType(StatusCodes.Status404NotFound)]
127+
[HttpDelete("{userName}")]
128+
public ActionResult Delete(string userName)
129+
{
130+
if (string.IsNullOrEmpty(userName)) return BadRequest();
131+
132+
133+
var customer = _customerService.DeleteCustomer(userName);
134+
if (customer == null) return NotFound();
135+
136+
_logger.LogInformation($"deleted customer with userName : {userName}!");
137+
138+
return Ok($"{userName} deleted !!!" );
139+
}
140+
}
141+
}

AuthorizeService/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
2+
#For more information, please see http://aka.ms/containercompat
3+
4+
FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1803 AS base
5+
WORKDIR /app
6+
EXPOSE 80
7+
8+
FROM microsoft/dotnet:2.1-sdk-nanoserver-1803 AS build
9+
WORKDIR /src
10+
COPY AuthorizeService/authorize.service.csproj AuthorizeService/
11+
RUN dotnet restore AuthorizeService/authorize.service.csproj
12+
COPY . .
13+
WORKDIR /src/AuthorizeService
14+
RUN dotnet build authorize.service.csproj -c Release -o /app
15+
16+
FROM build AS publish
17+
RUN dotnet publish authorize.service.csproj -c Release -o /app
18+
19+
FROM base AS final
20+
WORKDIR /app
21+
COPY --from=publish /app .
22+
ENTRYPOINT ["dotnet", "authorize.service.dll"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Authorize.Model;
2+
using AutoMapper;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace authorize.service.Helper
9+
{
10+
public class AutoMapperProfile : Profile
11+
{
12+
public AutoMapperProfile()
13+
{
14+
CreateMap<CustomerModel, Customer>();
15+
CreateMap<Customer, CustomerModel>();
16+
}
17+
}
18+
}

AuthorizeService/Model/Model.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Ocelot.JwtAuthorize;
7+
8+
namespace Authorize.Model
9+
{
10+
/// <summary>
11+
/// LoginModel
12+
/// </summary>
13+
public class LoginModel
14+
{
15+
/// <summary>
16+
/// UserName
17+
/// </summary>
18+
public string UserName { get; set; }
19+
}
20+
21+
public class Login
22+
{
23+
/// <summary>
24+
/// UserName
25+
/// </summary>
26+
[Required]
27+
public string UserName { get; set; }
28+
}
29+
/// <summary>
30+
/// CustomerModel
31+
/// </summary>
32+
public class CustomerModel
33+
{
34+
/// <summary>
35+
/// UserName
36+
/// </summary>
37+
[Required]
38+
public string UserName { get; set; }
39+
}
40+
41+
public class Customer
42+
{
43+
/// <summary>
44+
/// ID
45+
/// </summary>
46+
public int ID { get; set; }
47+
/// <summary>
48+
/// UserName
49+
/// </summary>
50+
[Required]
51+
public string UserName { get; set; }
52+
53+
}
54+
/// <summary>
55+
/// Response
56+
/// </summary>
57+
public class ResponseModel
58+
{
59+
/// <summary>
60+
/// result
61+
/// </summary>
62+
public bool Result{get; set;}
63+
public string Message{get; set;}
64+
/// <summary>
65+
/// token
66+
/// </summary>
67+
public TokenBuilder.Token Data{get; set;}
68+
}
69+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Authorize.Model;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace AuthorizeService.Persistence
5+
{
6+
public class ApplicationDbContext:DbContext
7+
{
8+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
9+
{
10+
11+
}
12+
public DbSet<Customer> Customers { get; set; }
13+
}
14+
}

AuthorizeService/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore;
2+
using Microsoft.AspNetCore.Hosting;
3+
4+
namespace Authorize
5+
{
6+
/// <summary>
7+
///
8+
/// </summary>
9+
public class Program
10+
{
11+
/// <summary>
12+
///
13+
/// </summary>
14+
/// <param name="args"></param>
15+
public static void Main(string[] args)
16+
{
17+
CreateWebHostBuilder(args).Build().Run();
18+
}
19+
/// <summary>
20+
///
21+
/// </summary>
22+
/// <param name="args"></param>
23+
/// <returns></returns>
24+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
25+
WebHost.CreateDefaultBuilder(args)
26+
//.UseUrls("http://localhost:7000")
27+
.UseStartup<Startup>();
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:58568",
7+
"sslPort": 0
8+
}
9+
},
10+
"$schema": "http://json.schemastore.org/launchsettings.json",
11+
"profiles": {
12+
"Docker": {
13+
"commandName": "Docker",
14+
"launchBrowser": true,
15+
"launchUrl": "",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
},
19+
"applicationUrl": "http://localhost:5000/"
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)