Example #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var allowedOrigin = context.OwinContext.Get <string>("as:clientAllowedOrigin") ?? "*";

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

            var userService = new UserOldService();
            var userDto     = new UserOld {
                UserName = context.UserName, Password = context.Password
            };
            var user = await userService.FindUser(userDto);

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


            string roles    = string.Join(",", user.Data.Roles.Select(x => x.Role).ToArray());
            var    identity = new ClaimsIdentity(context.Options.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Role, roles));

            var props = new AuthenticationProperties(new Dictionary <string, string>
            {
                {
                    "client_id", context.ClientId ?? string.Empty
                },
                {
                    "UserEmail", context.UserName
                },
                {
                    "UserId", user.Data.Id.ToString()
                },
                {
                    "Roles", roles
                }
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
        }
 public UserOldController()
 {
     _userServices = new UserOldService();
 }