Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication2.Custom_Models
{
public class tcompanycustom
{
public int CusId { get; set; }
public string Name { get; set; }
public string PhoneNo { get; set; }
public string Designation { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public object EmpPassword { get; internal set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;

namespace WebApplication2.Models
{
public partial class Tcompanydata
{
public int CusId { get; set; }
public string Name { get; set; }
public string PhoneNo { get; set; }
public string Designation { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Configuration;

namespace WebApplication2.Models
{
public partial class companyinfoContext : DbContext
{
//IConfiguration _config;
//public companyinfoContext(IConfiguration config)
//{
// _config = config;
//}

public companyinfoContext(DbContextOptions<companyinfoContext> options)
: base(options)
{
}

public virtual DbSet<Tcompanydata> Tcompanydata { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Tcompanydata>(entity =>
{
entity.HasKey(e => e.CusId);

entity.ToTable("tcompanydata");

entity.Property(e => e.CusId).HasColumnName("cus_id");

entity.Property(e => e.Designation)
.IsRequired()
.HasColumnName("designation")
.HasMaxLength(100)
.IsUnicode(false);

entity.Property(e => e.Email)
.IsRequired()
.HasColumnName("email")
.HasMaxLength(100)
.IsUnicode(false);

entity.Property(e => e.Name)
.IsRequired()
.HasColumnName("name")
.HasMaxLength(100)
.IsUnicode(false);

entity.Property(e => e.Password)
.IsRequired()
.HasColumnName("password")
.HasMaxLength(100)
.IsUnicode(false);

entity.Property(e => e.PhoneNo)
.IsRequired()
.HasColumnName("phone_no")
.HasMaxLength(100)
.IsUnicode(false);
});
}
}
}
24 changes: 24 additions & 0 deletions Deepak_Yadav/p_website/WebApplication2/WebApplication2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebApplication2
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:54841",
"sslPort": 44366
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApplication2": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
85 changes: 85 additions & 0 deletions Deepak_Yadav/p_website/WebApplication2/WebApplication2/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;

namespace WebApplication2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddDbContext<Models.companyinfoContext>(o => o.UseSqlServer(Configuration["dbConnection"]));
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
services.AddMvc();


});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}

app.UseCors(builder =>
builder.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin());
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseStaticFiles();
app.UseCookiePolicy();

app.UseMvc();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.9" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_SelectedScaffolderID>Microsoft.AspNet.Scaffolding.Mvc.RazorPages.ModelBasedRazorPageScaffolder</_SelectedScaffolderID>
<_SelectedScaffolderCategoryPath>root/Common/RazorPage</_SelectedScaffolderCategoryPath>
<WebStackScaffolding_ViewDialogWidth>600</WebStackScaffolding_ViewDialogWidth>
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
<WebStackScaffolding_LayoutPageFile />
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"jwt": {
"key": "abcdeepakyadav12345",
"Issuer": "Test.com"
},
"dbConnection": "server=CYG291;database=companyinfo;Trusted_Connection=True;"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using WebApplication2.Custom_Models;

namespace WebApplication2.controllers
{
[Route("api/[controller]")]
[ApiController]
public class LoginController : ControllerBase
{
public IConfiguration _configuration;
public Models.companyinfoContext _cSharpTrainingContext;
public LoginController(IConfiguration config, WebApplication2.Models.companyinfoContext context)
{
_configuration = config;
_cSharpTrainingContext = context;
}

[AllowAnonymous]
[HttpPost]
public IActionResult Login([FromBody]Custom_Models.tcompanycustom loginRequest)
{
IActionResult response = Unauthorized();
var user = AuthenticateUser(loginRequest);

if (user != null)
{
var tokenString = GenerateJSONWebToken(user);
response = Ok(new { token = tokenString });
}

return response;
}


private string GenerateJSONWebToken(WebApplication2.Custom_Models.tcompanycustom loginInfo)
{
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

var claims = new[] {
new Claim(JwtRegisteredClaimNames.Sub, _configuration["Jwt:Issuer"]),
new Claim("Name", loginInfo.Name),
new Claim("Email", loginInfo.Email),
new Claim("PhoneNo", loginInfo.PhoneNo),
new Claim("Designation",loginInfo.Designation)
};

var token = new JwtSecurityToken(_configuration["Jwt:Issuer"],
_configuration["Jwt:Issuer"],
claims,
expires: DateTime.Now.AddMinutes(120),
signingCredentials: credentials);

return new JwtSecurityTokenHandler().WriteToken(token);
}
private WebApplication2.Custom_Models.tcompanycustom AuthenticateUser(tcompanycustom login)
{
WebApplication2.Custom_Models.tcompanycustom user = null;

Models.Tcompanydata employee = _cSharpTrainingContext.Tcompanydata.Where(x => x.Email == login.Email && x.Password == login.Password).FirstOrDefault();
if (employee != null)
{
user = new WebApplication2.Custom_Models.tcompanycustom { Name = employee.Name, Email = employee.Email, PhoneNo = employee.PhoneNo,Designation=employee.Designation };
}
return user;
}
}
}
Loading