Skip to content

Commit dd8d193

Browse files
AdminAdmin
Admin
authored and
Admin
committed
Add project files.
1 parent 2d5a08a commit dd8d193

36 files changed

+6216
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.AspNetCore.Mvc;
5+
using SecureWebSite.Server.Models;
6+
using System.Security.Claims;
7+
8+
namespace SecureWebSite.Server.Controllers
9+
{
10+
[Route("api/securewebsite")]
11+
[ApiController]
12+
public class SecureWebsiteController(SignInManager<User> sm, UserManager<User> um) : ControllerBase
13+
{
14+
private readonly SignInManager<User> signInManager = sm;
15+
private readonly UserManager<User> userManager = um;
16+
17+
[HttpPost("register")]
18+
public async Task<ActionResult> RegisterUser(User user)
19+
{
20+
21+
IdentityResult result = new ();
22+
23+
try {
24+
User user_ = new User(){
25+
Name = user.Name,
26+
Email = user.Email,
27+
UserName = user.UserName,
28+
};
29+
30+
result = await userManager.CreateAsync(user_, user.PasswordHash);
31+
32+
if(!result.Succeeded){
33+
return BadRequest(result);
34+
}
35+
} catch(Exception ex) {
36+
return BadRequest("Something went wrong, please try again. " + ex.Message);
37+
}
38+
39+
return Ok(new { message = "Registered Successfully.", result = result });
40+
}
41+
42+
[HttpPost("login")]
43+
public async Task<ActionResult> LoginUser(Login login)
44+
{
45+
46+
try
47+
{
48+
User user_ = await userManager.FindByEmailAsync(login.Email);
49+
if(user_ != null){
50+
login.Username = user_.UserName;
51+
52+
if(!user_.EmailConfirmed){
53+
user_.EmailConfirmed = true;
54+
}
55+
56+
var result = await signInManager.PasswordSignInAsync(user_, login.Password, login.Remember, false);
57+
58+
if (!result.Succeeded)
59+
{
60+
return Unauthorized(new {message = "Check your login credentials and try again" });
61+
}
62+
63+
user_.LastLogin = DateTime.Now;
64+
var updateResult = await userManager.UpdateAsync(user_);
65+
} else {
66+
return BadRequest(new {message = "Please check your credentials and try again. " });
67+
}
68+
}
69+
catch (Exception ex)
70+
{
71+
return BadRequest(new {message = "Something went wrong, please try again. " + ex.Message });
72+
}
73+
74+
return Ok(new { message = "Login Successful." });
75+
}
76+
77+
[HttpGet("logout"), Authorize]
78+
public async Task<ActionResult> LogoutUser(){
79+
80+
try {
81+
await signInManager.SignOutAsync();
82+
} catch (Exception ex) {
83+
return BadRequest(new {message = "Someting went wrong, please try again. " + ex.Message });
84+
}
85+
86+
return Ok(new { message = "You are free to go!" });
87+
}
88+
89+
[HttpGet("admin"), Authorize]
90+
public ActionResult AdminPage(){
91+
string[] partners = { "Raja", "Bill Gates", "Elon Musk", "Taylor Swift", "Jeff Bezoss",
92+
"Mark Zuckerberg", "Joe Biden", "Putin"};
93+
94+
return Ok(new { trustedPartners = partners });
95+
}
96+
97+
[HttpGet("home/{email}"), Authorize]
98+
public async Task<ActionResult> HomePage(string email)
99+
{
100+
User userInfo = await userManager.FindByEmailAsync(email);
101+
if (userInfo == null){
102+
return BadRequest(new { message = "Something went wrong, please try again." });
103+
}
104+
105+
return Ok(new { userInfo = userInfo });
106+
}
107+
108+
[HttpGet("xhtlekd")]
109+
public async Task<ActionResult> CheckUser()
110+
{
111+
User currentuser = new();
112+
113+
try {
114+
var user_ = HttpContext.User;
115+
var principals = new ClaimsPrincipal(user_);
116+
var result = signInManager.IsSignedIn(principals);
117+
if (result){
118+
currentuser = await signInManager.UserManager.GetUserAsync(principals);
119+
} else {
120+
return Forbid();
121+
}
122+
} catch (Exception ex) {
123+
return BadRequest(new {message = "Something went wrong please try again. " + ex.Message });
124+
}
125+
126+
return Ok(new {message = "Logged in", user = currentuser});
127+
}
128+
129+
}
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace SecureWebSite.Server.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class WeatherForecastController : ControllerBase
8+
{
9+
private static readonly string[] Summaries = new[]
10+
{
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
13+
14+
private readonly ILogger<WeatherForecastController> _logger;
15+
16+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
[HttpGet]
22+
public IEnumerable<WeatherForecast> Get()
23+
{
24+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
25+
{
26+
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
27+
TemperatureC = Random.Shared.Next(-20, 55),
28+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
29+
})
30+
.ToArray();
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore;
3+
using SecureWebSite.Server.Models;
4+
5+
namespace SecureWebSite.Server.Data
6+
{
7+
public class ApplicationDbContext : IdentityDbContext<User>
8+
{
9+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
10+
}
11+
}

0 commit comments

Comments
 (0)