コード例 #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            try
            {
                IIdentityManager identityManager = _identityManagerFactory.Invoke();
                if (identityManager == null)
                {
                    throw new NullReferenceException($"{nameof(identityManager)} can not be found!");
                }

                User user = await identityManager.FindAsync(context.UserName, context.Password);

                if (user == null)
                {
                    context.SetError(INVALID_GRANT, "Invalid username and password");
                    return;
                }

                ICollection <string> inRoles = await identityManager.GetRolesAsync(user.Id);

                // oAuth
                var oAuthIdentity = ClaimsIdentityFactory.Create(user, OAuthDefaults.AuthenticationType, inRoles.ToArray());

                AuthenticationProperties properties = CreateProperties();
                properties.Dictionary.Add("as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId);
                properties.Dictionary.Add("username", context.UserName);

                var ticket = new AuthenticationTicket(oAuthIdentity, properties);
                context.Validated(ticket);
            }
            catch (Exception ex)
            {
                // TODO: some error logging

                throw;
            }
        }