Ejemplo n.º 1
0
        public IHttpActionResult Login(LoginModel loginModel)
        {
            try
            {
                if (string.IsNullOrEmpty(loginModel.userName) || string.IsNullOrEmpty(loginModel.password))
                {
                    return(BadRequest(Fly.Resources.OperationLP.InvalidUserNamePassword));
                }

                loginModel.password = WebUI.Helpers.WebUiUtility.Encrypt(loginModel.password);
                var pairs = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("grant_type", "password"),
                    new KeyValuePair <string, string>("username", loginModel.userName),
                    new KeyValuePair <string, string> ("Password", loginModel.password)
                };
                var content = new FormUrlEncodedContent(pairs);
                ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

                var authorizationHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes("rajeev:" + loginModel.password));


                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; // comparable to modern browsers

                using (var client = new HttpClient())
                {
                    var response = client.PostAsync(new Uri(System.Configuration.ConfigurationManager.AppSettings["ServiceUrl"].ToString() + "Token"), content).Result;

                    var token = response.Content.ReadAsAsync <Token>(new[] { new JsonMediaTypeFormatter() }).Result;
                    using (SecurityUserRepository obj = new SecurityUserRepository())
                    {
                        SecurityUser secUserModel = obj.GetBy(loginModel.userName, loginModel.password);
                        if (secUserModel != null)
                        {
                            token.UserId   = secUserModel.PayMobSendId;
                            token.Tocken   = secUserModel.TockenToP;
                            token.UserName = secUserModel.FullName;
                        }
                    }
                    // var sss = response.Content.ReadAsStringAsync().Result;
                    //return Json(new { tock = sss });
                    return(Ok(token));
                }
            }
            catch (OperationCanceledException oce)
            {
                logger.Error(oce.Message + " < " + oce.InnerException.Message + " < " + oce.StackTrace + " == " + oce.Data);
                return(Ok(new { success = false, access_token = "" }));
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message + " > " + ex.InnerException.Message + " > " + ex.StackTrace);
                return(Ok(new { success = false, access_token = "" }));
            }
        }
Ejemplo n.º 2
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin");

            if (allowedOrigin == null)
            {
                allowedOrigin = "*";
            }

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });



            /*** Replace below user authentication code as per your Entity Framework Model ****/
            SecurityUser secUserModel = new SecurityUser();

            using (SecurityUserRepository obj = new SecurityUserRepository())
            {
                secUserModel = obj.GetBy(context.UserName, context.Password);
                if (secUserModel == null)
                {
                    context.SetError("invalid_grant",
                                     OperationLP.InvalidUserNamePassword);
                    return;
                }
            }


            ClaimsIdentity oAuthIdentity =
                new ClaimsIdentity(context.Options.AuthenticationType);
            ClaimsIdentity cookiesIdentity =
                new ClaimsIdentity(context.Options.AuthenticationType);

            Claim newClaim = new Claim(ClaimTypes.Role, secUserModel.SecurityUserRole.FirstOrDefault().SecurityRole.RoleNameE);

            newClaim.Properties.Add(new KeyValuePair <string, string>("UserId", secUserModel.Id.ToString()));
            oAuthIdentity.AddClaim(new Claim("UserId", secUserModel.Id.ToString()));
            oAuthIdentity.AddClaim(newClaim);
            //  oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, "Supervisor"));

            AuthenticationProperties properties = CreateProperties(context.UserName);
            AuthenticationTicket     ticket     =
                new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }