关于 SimpleMembership 中 CreateDate 的问题

使用 WebMatrix.WebData.WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
                                                                   new
                                                                       {
                                                                           Email = model.UserName,
                                                                           model.NickName,
                                                                           model.Sex,
                                                                           model.Province,
                                                                           model.City,
                                                                           model.ProvinceName,
                                                                           model.CityName,
                                                                           RegIp = Dev.Comm.Web.HostHelper.getRealIp()
                                                                       }, true);

 

创建用户,在数据库中记录不是当前时区的时间,记录的 UTC 时间 (http://zh.wikipedia.org/zh/%E5%8D%8F%E8%B0%83%E4%B8%96%E7%95%8C%E6%97%B6),所以这SB玩意记录的是0时区的的时间, 在取出后,所以与当前时间差8小时, 北京时间为+8时区。

 

 

 

下面反编后的代码片断。

 

// WebMatrix.WebData.SimpleMembershipProvider
/// <summary>Creates a new user account by using the specified user name and password.</summary>
/// <returns>A token that can be sent to the user to confirm the user account.</returns>
/// <param name="userName">The user name.</param>
/// <param name="password">The password.</param>
/// <param name="requireConfirmationToken">(Optional) true to specify that the user account must be confirmed; otherwise, false. The default is false.</param>
/// <exception cref="T:System.Web.Security.MembershipCreateUserException">
///   <paramref name="username" /> is empty.-or-<paramref name="username" /> already has a user account.-or-<paramref name="password" /> is empty.-or-<paramref name="password" /> is longer than 128 characters.-or-A user record that corresponds to <paramref name="username" /> does not exist in the <see cref="P:WebMatrix.WebData.SimpleMembershipProvider.UserTableName" /> table (the user profile table).-or-The database operation failed.</exception>
/// <exception cref="T:System.InvalidOperationException">The <see cref="T:WebMatrix.WebData.SimpleMembershipProvider" /> class was not initialized using a call to the <see cref="Overload:WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection" /> method.</exception>
public override string CreateAccount(string userName, string password, bool requireConfirmationToken)
{
    this.VerifyInitialized();
    if (password.IsEmpty())
    {
        throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
    }
    string text = Crypto.HashPassword(password);
    if (text.Length > 128)
    {
        throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
    }
    if (userName.IsEmpty())
    {
        throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);
    }
    string result;
    using (IDatabase database = this.ConnectToDatabase())
    {
        int userId = SimpleMembershipProvider.GetUserId(database, this.SafeUserTableName, this.SafeUserNameColumn, this.SafeUserIdColumn, userName);
        if (userId == -1)
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
        }
        object arg = database.QuerySingle("SELECT COUNT(*) FROM [" + SimpleMembershipProvider.MembershipTableName + "] WHERE UserId = @0", new object[]
        {
            userId
        });
        if (SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site23 == null)
        {
            SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site23 = CallSite<Func<CallSite, object, bool>>.Create(Binder.UnaryOperation(CSharpBinderFlags.None, ExpressionType.IsTrue, typeof(SimpleMembershipProvider), new CSharpArgumentInfo[]
            {
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
            }));
        }
        Func<CallSite, object, bool> arg_191_0 = SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site23.Target;
        CallSite arg_191_1 = SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site23;
        if (SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site24 == null)
        {
            SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site24 = CallSite<Func<CallSite, object, int, object>>.Create(Binder.BinaryOperation(CSharpBinderFlags.None, ExpressionType.GreaterThan, typeof(SimpleMembershipProvider), new CSharpArgumentInfo[]
            {
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.Constant, null)
            }));
        }
        Func<CallSite, object, int, object> arg_18C_0 = SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site24.Target;
        CallSite arg_18C_1 = SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site24;
        if (SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site25 == null)
        {
            SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site25 = CallSite<Func<CallSite, object, int, object>>.Create(Binder.GetIndex(CSharpBinderFlags.None, typeof(SimpleMembershipProvider), new CSharpArgumentInfo[]
            {
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
                CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.Constant, null)
            }));
        }
        if (arg_191_0(arg_191_1, arg_18C_0(arg_18C_1, SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site25.Target(SimpleMembershipProvider.<CreateAccount>o__SiteContainer22.<>p__Site25, arg, 0), 0)))
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
        }
        string text2 = null;
        object obj = DBNull.Value;
        if (requireConfirmationToken)
        {
            text2 = SimpleMembershipProvider.GenerateToken();
            obj = text2;
        }
        int num = 0;
        int num2 = database.Execute("INSERT INTO [" + SimpleMembershipProvider.MembershipTableName + "] (UserId, [Password], PasswordSalt, IsConfirmed, ConfirmationToken, CreateDate, PasswordChangedDate, PasswordFailuresSinceLastSuccess) VALUES (@0, @1, @2, @3, @4, @5, @5, @6)", new object[]
        {
            userId,
            text,
            string.Empty,
            !requireConfirmationToken,
            obj,
            DateTime.UtcNow,
            num
        });
        if (num2 != 1)
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
        }
        result = text2;
    }
    return result;
}

解决方案:

  /// <summary>
        /// 将UTC 时间 转化为本地化时间
        /// </summary>
        /// <param name="utcDate"></param>
        /// <returns></returns>
        public static DateTime UtcToLocal(DateTime utcDate)
        {
            DateTime convertedDate = DateTime.SpecifyKind(utcDate, DateTimeKind.Utc);
 
            var localtime = convertedDate.ToLocalTime();
 
            return localtime;
        }
 
// TEST
[TestClass]
    public class DateUtilTest
    {
        [TestMethod]
        public void UtcToLocal()
        {
            var UtcDate = new DateTime(2013, 1, 1, 1, 1, 1);
 
            DateTime converted = DateUtil.UtcToLocal(UtcDate);
            Assert.AreEqual(UtcDate.AddHours(8), converted);
 
        }
    }

 

 

已经更新至 https://github.com/zbw911/Dev.All/tree/master/DLL%20Release 最新dll

你可能感兴趣的:(create)