public HttpResponseMessage LoginCust([FromBody] CustomerInfo loginInfo)
        {
            //System.Diagnostics.EventLog.WriteEntry("Application", "LoginCust started");
            AccountManagementService    accService  = new AccountManagementService();
            HttpStatusCode              ReturnCode  = HttpStatusCode.OK;
            TranInfo <CustomerInfo>     transaction = new TranInfo <CustomerInfo>();
            ReturnResult <CustomerInfo> result      = new ReturnResult <CustomerInfo>();

            try
            {
                result = accService.GetCustomer(loginInfo);
                if (result.status.Status == StatusEnum.Success)
                {
                    transaction.data   = result.result;
                    transaction.status = true;
                }
                else
                {
                    // transaction.warnings.Add(result.status.StatusMessage);

                    transaction.status = false;
                    transaction.AddException(result.status.StatusMessage);

                    //_commonService.LogError(result.status.StatusCode, result.status.StatusMessage, result.status.StatusDetails, 0, "LoginCust");
                }
            }
            catch (Exception ex)
            {
                transaction.status = false;
                transaction.AddException(ex.Message);
                ReturnCode    = HttpStatusCode.InternalServerError;
                result.status = _eHandler.HandleException(ex);
                _commonService.LogError(result.status);
                // transaction.AddException(ex.Message);
                //ReturnCode = HttpStatusCode.InternalServerError;

                //if (ex.InnerException != null) { InnerExceptionMessage = ex.InnerException.Message; }
                //LogHelper.Instance.Error(CorrelationID, Username, Request.GetRequestContext().VirtualPathRoot, ex.Message, InnerExceptionMessage, 0, ex);
            }

            return(Request.CreateResponse <TranInfo <CustomerInfo> >(ReturnCode, transaction));
        }
        public HttpResponseMessage GetCustomer(int custID)
        {
            AccountManagementService    accService  = new AccountManagementService();
            HttpStatusCode              ReturnCode  = HttpStatusCode.OK;
            TranInfo <CustomerInfo>     transaction = new TranInfo <CustomerInfo>();
            ReturnResult <CustomerInfo> result      = new ReturnResult <CustomerInfo>();

            try
            {
                result = accService.GetCustomer(custID);
                if (result.status.Status == StatusEnum.Success)
                {
                    transaction.data   = result.result;
                    transaction.status = true;
                }
                else
                {
                    // transaction.warnings.Add(result.status.StatusMessage);

                    transaction.status = false;
                    transaction.AddException(result.status.StatusMessage);

                    //_commonService.LogError(result.status.StatusCode, result.status.StatusMessage, result.status.StatusDetails, 0, "LoginCust");
                }
            }
            catch (Exception ex)
            {
                transaction.AddException(ex.Message);
                ReturnCode    = HttpStatusCode.InternalServerError;
                result.status = _eHandler.HandleException(ex);
                _commonService.LogError(result.status);

                //  LogHelper.Instance.Error(service.CorrelationId, Username, Request.GetRequestContext().VirtualPathRoot, ex.Message, InnerExceptionMessage, 0, ex);
            }

            return(Request.CreateResponse <TranInfo <CustomerInfo> >(ReturnCode, transaction));
        }
Exemplo n.º 3
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            string Username = string.Empty, Roles = string.Empty, CorrelationId = Guid.NewGuid().ToString();

            Username = context.UserName;
            //using owincontext
            if (string.IsNullOrEmpty(Username))
            {
                Username = HttpContext.Current.GetOwinContext().Request.User.Identity.Name;
            }

            CustomerInfo cust = new CustomerInfo();

            cust.email    = Username;
            cust.Password = context.Password;


            AccountManagementService    accService = new AccountManagementService();
            ReturnResult <CustomerInfo> result     = new ReturnResult <CustomerInfo>();

            result = accService.GetCustomer(cust);
            if (result.status.Status != StatusEnum.Success)
            {
                return;
            }


            //using windowsidentity
            //if (string.IsNullOrEmpty(Username))
            //{
            //    Username = WindowsIdentity.GetCurrent().Name;
            //}
            ////extract the username excluding domain name
            //Username = Username.Contains(@"\") ? Username.Substring(Username.LastIndexOf(@"\") + 1) : Username;
            //Username = Username.ToLower();

            ////hardcoded values for test
            //List<Role> roles = new List<Role>();
            //roles.Add(new Role() { Id = 2400, Name = "Viewer", Description = "Viewer" });
            //roles.Add(new Role() { Id = 2401, Name = "Originator", Description = "Originator" });
            //Roles = string.Join(",", from item in roles select item.Id.ToString());

            //check if the user has access
            //APIHelper api = new APIHelper();
            ////IList<int> roles = api.GetUserRoles(Username, CorrelationId);
            //IList<int> roles = new List<int>();
            //roles.Add(2600);



            string UserID = Convert.ToString(result.result.User.UserID);
            //if user has access generate token with Username and Roles in claims
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, Username));
            //identity.AddClaim(new Claim(ClaimTypes.Role, Roles));
            identity.AddClaim(new Claim(ClaimTypes.SerialNumber, CorrelationId));
            identity.AddClaim(new Claim(ClaimTypes.UserData, UserID));
            // identity.AddClaim(new Claim(ClaimTypes.GivenName, result.result.User.FirstName));
            AuthenticationProperties properties = CreateProperties(Username, result.result.User.FirstName, UserID);

            AuthenticationTicket ticket = new AuthenticationTicket(identity, properties);
            await Task.Run(() => context.Validated(ticket));
        }