using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
namespace TestProject.Controllers
{
/// <summary>
/// 홈 컨트롤러
/// </summary>
public class HomeController : Controller
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 인덱스 페이지 처리하기 - Index()
/// <summary>
/// 인덱스 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public IActionResult Index()
{
return View();
}
#endregion
#region 비밀 페이지 처리하기 - Secret()
/// <summary>
/// 비밀 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
[Authorize]
public IActionResult Secret()
{
return View();
}
#endregion
#region 관리자 페이지 처리하기 - Administrator(authorizationService)
/// <summary>
/// 관리자 페이지 처리하기
/// </summary>
/// <param name="authorizationService">권한 서비스</param>
/// <returns>액션 결과</returns>
public async Task<IActionResult> Administrator([FromServices] IAuthorizationService authorizationService)
{
AuthorizationPolicyBuilder builder = new AuthorizationPolicyBuilder("Schema");
AuthorizationPolicy policy = builder.RequireClaim(ClaimTypes.Role, "Administrator").Build();
AuthorizationResult result = await authorizationService.AuthorizeAsync(User, policy);
if(!result.Succeeded)
{
return Forbid();
}
return View();
}
#endregion
#region 로그인 페이지 처리하기 - Login(returnURL)
/// <summary>
/// 로그인 페이지 처리하기
/// </summary>
/// <param name="returnURL">반환 URL</param>
/// <returns>액션 결과</returns>
[HttpGet]
public IActionResult Login(string returnURL = null)
{
ViewData["ReturnURL"] = returnURL;
return View();
}
#endregion
#region 로그인 페이지 처리하기 - Login(userName, password, returnURL)
/// <summary>
/// 로그인 페이지 처리하기
/// </summary>
/// <param name="userName">사용자명</param>
/// <param name="password">패스워드</param>
/// <param name="returnURL">반환 URL</param>
/// <returns>액션 결과</returns>
[HttpPost]
public async Task<IActionResult> Login(string userName, string password, string returnURL)
{
if(userName == "홍길동" && password == "1234")
{
List<Claim> personClaimList = new List<Claim>()
{
new Claim(ClaimTypes.Name , "홍길동" ),
new Claim(ClaimTypes.Gender , "남성" ),
new Claim(ClaimTypes.DateOfBirth, "2000-01-01" ),
new Claim(ClaimTypes.HomePhone , "02-700-1000" ),
new Claim(ClaimTypes.MobilePhone, "010-3000-4000" ),
new Claim(ClaimTypes.Email , "hkd@daum.net" ),
new Claim(ClaimTypes.Country , "한국" ),
new Claim(ClaimTypes.PostalCode , "300-400" ),
new Claim(ClaimTypes.Role , "User" )
};
List<Claim> licenseClaimList = new List<Claim>()
{
new Claim(ClaimTypes.Name , "홍길동"),
new Claim("License" , "1급" )
};
ClaimsIdentity personClaimsIdentity = new ClaimsIdentity(personClaimList , "개인");
ClaimsIdentity licenseClaimsIdentity = new ClaimsIdentity(licenseClaimList, "정부");
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal
(
new []
{
personClaimsIdentity,
licenseClaimsIdentity
}
);
await HttpContext.SignInAsync("CookieAuthentication", claimsPrincipal);
if(returnURL == null)
{
return RedirectToAction("Index");
}
else
{
return Redirect(returnURL);
}
}
else
{
ViewData["Message"] = "등록되지 않은 사용자 입니다.";
return View();
}
}
#endregion
#region 로그아웃 페이지 처리하기 - Logout()
/// <summary>
/// 로그아웃 페이지 처리하기
/// </summary>
/// <returns>액션 결과 태스크</returns>
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync("CookieAuthentication");
return RedirectToAction("Index");
}
#endregion
#region 권한 없음 페이지 처리하기 - NoAuthorized()
/// <summary>
/// 권한 없음 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public IActionResult NoAuthorized()
{
return View();
}
#endregion
}
}