Exemplo n.º 1
0
        public async Task <LoginResultModel> Login(UserLoginModel login)
        {
            LoginResultModel result = new LoginResultModel();

            var ssoToken = _ssoHelper.Login(login.Username, login.Password, "PETRONAS");

            if (ssoToken == null || ssoToken.Result == Guid.Empty)
            {
                result.Message = Common.Constants.ResponseMessage.UnSuccessLogin;
                result.Status  = false;
            }
            else
            {
                result.Message = Common.Constants.ResponseMessage.Success;
                result.Status  = true;
            }

            result.Token = ssoToken.Result;
            return(result);
        }
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            ApplicationUser user;
            var             allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin");

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

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

            if (context.UserName.IndexOf("@petronas.com") >= 0)
            {
                var       request   = new LoginRequest();
                SSOHelper sSOHelper = new SSOHelper();
                var       ssoToken  = sSOHelper.Login(context.UserName, context.Password, "Petronas");
                if (ssoToken == null || ssoToken.Result == Guid.Empty)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }
                user = await _repo.FindUser(context.UserName);
            }
            else
            //using (AuthRepository _repo = new AuthRepository())
            {
                user = await _repo.FindUser(context.UserName, context.Password);

                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }
            }
            var roles    = _repo.GetUserRole(user.Id.ToString());
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Role, Newtonsoft.Json.JsonConvert.SerializeObject(roles)));
            identity.AddClaim(new Claim("sub", context.UserName));

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                {
                    "as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId
                },
                {
                    "userName", context.UserName
                },
                {
                    "fullname", user.FirstName + " " + user.LastName
                },
                {
                    "roles", Newtonsoft.Json.JsonConvert.SerializeObject(roles)
                }
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
        }