//public Audit Audit { get; set; }

        public IdentityUser()
        {
            Profile = new IdentityProfile();
        }
示例#2
0
        public Task <TUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
        {
            IdentityUser        user    = null;
            IdentityProfile     profile = null;
            List <IdentityRole> roles   = null;

            using (var connection = new SqlConnection(_connection))
            {
                using (var cmd = new SqlCommand())
                {
                    if (connection.State == System.Data.ConnectionState.Closed)
                    {
                        connection.Open();
                    }
                    cmd.Connection  = connection;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "usp_iden_FindUserById";
                    cmd.Parameters.AddWithValue("@USERID", userId);

                    using (var reader = cmd.ExecuteReader())
                    {
                        int resultIndex = 0;

                        while (reader.HasRows)
                        {
                            if (resultIndex == 0)
                            {
                                user = new IdentityUser();
                            }
                            else if (resultIndex == 1)
                            {
                                profile = new IdentityProfile();
                            }
                            else
                            {
                                roles = new List <IdentityRole>();
                            }
                            while (reader.Read())
                            {
                                if (resultIndex == 0)
                                {
                                    user.UserId               = Convert.ToString(reader["UserId"]);
                                    user.Email                = Convert.ToString(reader["Email"]);
                                    user.EmailConfirmed       = Convert.ToBoolean(reader["EmailConfirmed"]);
                                    user.PasswordHash         = Convert.ToString(reader["PasswordHash"]);
                                    user.SecurityStamp        = Convert.ToString(reader["SecurityStamp"]);
                                    user.PhoneNumber          = Convert.ToString(reader["PhoneNumber"]);
                                    user.PhoneNumberConfirmed = Convert.ToBoolean(reader["PhoneNumberConfirmed"]);
                                    user.TwoFactorEnabled     = Convert.ToBoolean(reader["TwoFactorEnabled"]);
                                    var lockoutDateTime = Convert.ToDateTime(reader["LockoutEndDateUtc"]);
                                    lockoutDateTime        = DateTime.SpecifyKind(lockoutDateTime, DateTimeKind.Utc);
                                    user.LockoutEndDateUtc = lockoutDateTime;
                                    user.LockoutEnabled    = Convert.ToBoolean(reader["LockoutEnabled"]);
                                    user.AccessFailedCount = Convert.ToInt32(reader["AccessFailedCount"]);
                                    user.UserName          = Convert.ToString(reader["UserName"]);
                                    //user.Audit = new Audit(reader.GetGuid(reader.GetOrdinal("CreateBy")));
                                    //user.Audit.CreateDate = Convert.ToDateTime(reader["CreateDate"]);
                                    //user.Audit.ModifyBy = reader.GetGuid(reader.GetOrdinal("ModifyBy"));
                                    //user.Audit.ModifyDate = Convert.ToDateTime(reader["ModifyDate"]);
                                }
                                else if (resultIndex == 1)
                                {
                                    profile.UserId     = reader.GetGuid(reader.GetOrdinal("UserId"));
                                    profile.FirstName  = Convert.ToString(reader["FirstName"]);
                                    profile.MiddleName = Convert.ToString(reader["MiddleName"]);
                                    profile.LastName   = Convert.ToString(reader["LastName"]);
                                }
                                else
                                {
                                    roles.Add(new IdentityRole
                                    {
                                        RoleId = reader.GetGuid(reader.GetOrdinal("RoleId")),
                                        Name   = Convert.ToString(reader["Name"])
                                    });
                                }
                            }

                            resultIndex++;
                            reader.NextResult();
                        }

                        if (user != null)
                        {
                            user.Profile = profile;
                            //user.Roles = roles;
                        }
                    }
                    return(Task.FromResult((TUser)user));
                }
            }
        }