Exemplo n.º 1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var loginBusinessModel = new LoginDataContract {
                Email = context.UserName, PasswordHash = context.Password
            };
            var user = await _loginBusiness.FindByUserNameorEmail(loginBusinessModel);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
            loginBusinessModel.UserId = user.UserId;
            var roles = await _loginBusiness.GetRolesByUserId(loginBusinessModel);

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

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString()));
            identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
            foreach (var item in roles)
            {
                identity.AddClaim(new Claim(ClaimTypes.Role, item.Name));
            }

            context.Validated(identity);
        }