예제 #1
0
        private void ValidateUserAndProceed(string name, string email, OAuthGrantCustomExtensionContext context)
        {
            IdentityUser user     = null;
            string       userRole = Enums.Role.Guests.ToString();

            Domain.UserService userService = new Domain.UserService();

            if (userService.ValidateAndCreateUser(email, name))
            {
                user     = new IdentityUser(email);
                userRole = userService.GetUserRole(email).ToString();
            }
            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }


            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            identity.AddClaim(new Claim("sub", user.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Role, userRole));

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                {
                    "as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId
                },
                {
                    "userName", email
                }
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
        }
예제 #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 });
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });

            bool   flagVerify = true;
            string strCode    = ConfigurationManager.AppSettings["AdminUserCode"].ToString();

            IdentityUser user     = null;
            string       userRole = Enums.Role.Guests.ToString();

            //await _repo.FindUser(context.UserName, context.Password);
            Domain.UserService userService = new Domain.UserService();

            if (userService.ValidateUser(context.UserName, context.Password))
            {
                user     = new IdentityUser(context.UserName);
                userRole = userService.GetUserRole(context.UserName).ToString();
            }

            if (userRole == Enums.Role.Administrators.ToString() || userRole == Enums.Role.SalesSupport.ToString())
            {
                if (context.Scope != null && context.Scope.Count > 0 && context.Scope[0] == strCode)
                {
                    flagVerify = true;
                }
                else
                {
                    flagVerify = false;
                    user       = null;
                }
            }

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }


            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim("sub", context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Role, userRole));

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                {
                    "as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId
                },
                {
                    "userName", context.UserName
                }
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
        }