using ARC.GMP.Models; using ARC.GMP.Types; using ARC.GMP.Utils; using Newtonsoft.Json; using NLog; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Http; using System.Security.Principal; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; using System.Web.Script.Serialization; namespace ARC.GMP.Controllers { public class UserAccessController : BaseController { public async Task ViewProfile() { AuthenticationDetail authDetail = new AuthenticationDetail(); string userAuthenticateUrl = ConfigurationManager.AppSettings.Get("UserAuthenticateUrl"); string userAuthorizeUrl = ConfigurationManager.AppSettings.Get("UserAuthorizeUrl"); try { _logger.Log(LogLevel.Trace, "View Profile Start.."); _logger.Log(LogLevel.Trace, "AuthenticateURL: " + userAuthenticateUrl); _logger.Log(LogLevel.Trace, "AuthorizeURL: " + userAuthorizeUrl); //Authentication var identity = User.Identity as WindowsIdentity; if (identity == null) { _logger.Log(LogLevel.Trace, "**** User Identity is null ****"); return Json(new HttpResponseMessage(HttpStatusCode.Unauthorized), JsonRequestBehavior.AllowGet); } string userName = identity.Name; // Harcoding the username to work local deployment //userName = "RC\\ajith.alex"; _logger.Log(LogLevel.Trace, "Username: " + userName); var fullUserName = userName.Split(new char[] { '\\' }); if (fullUserName != null && fullUserName.Length == 2) { userName = fullUserName[1].Trim(); } authDetail = await AccessPermissionManager.IsAuthenticatedAsync(userAuthenticateUrl, userName); //bool isAuthenticated = AccessPermissionManager.IsAuthenticated(userAuthenticateUrl, userName, out authDetail); //if (isAuthenticated) if (authDetail != null && authDetail.IsAuthenticated) { //Authorization Tuple authorizationDetail = await AccessPermissionManager.IsAuthorizedAsync(userAuthorizeUrl, Token, authDetail, ClientId); if (authorizationDetail.Item1) { _logger.Log(LogLevel.Trace, "View Profile End.."); return Json(authorizationDetail.Item2, JsonRequestBehavior.AllowGet); } else { _logger.Log(LogLevel.Trace, "**** User cannot be authorized from authorization URL ****"); return Json(new HttpResponseMessage(HttpStatusCode.Unauthorized), JsonRequestBehavior.AllowGet); } } else { _logger.Log(LogLevel.Trace, "**** User cannot be authenticated from authentication URL ****"); return Json(new HttpResponseMessage(HttpStatusCode.Unauthorized), JsonRequestBehavior.AllowGet); } } catch (Exception ex) { _logger.Log(LogLevel.Trace, ex.Message); _logger.Log(LogLevel.Trace, ex.StackTrace); return Json(new HttpResponseMessage(HttpStatusCode.Unauthorized), JsonRequestBehavior.AllowGet); } } } }