gpt4 book ai didi

c# - SqlMembershipProvider Membership.GetUser 按名称失败

转载 作者:太空狗 更新时间:2023-10-29 21:54:37 24 4
gpt4 key购买 nike

问题如果我使用 GUID,Membership.GetUser 工作正常,但如果我在没有参数或名称的情况下尝试它会失败:

//This works and adds records to aspnet_user & aspnet_Membership tables
MembershipUser membershipUser = Membership.CreateUser(_name, _pwd, _email, null, null, true, null, out createStatus);

//These fail with an InvalidCastException
MembershipUser membershipUser2 = Membership.GetUser(_name, true);
MembershipUser membershipUser3 = Membership.GetUser();

//If I go look up the GUID for this user (userId), this works!
MembershipUser membershipUser4 = Membership.GetUser(new Guid("E1428CD3-CF17-494E-AB77-CF8F6010F585"), true);

其他谜团

//This works
Int32 count = Membership.GetNumberOfUsersOnline();

//this fails (but totalRecords has the right value)
Int32 totalRecords;
MembershipUserCollection col = Membership.GetAllUsers(0,100, out totalRecords);

设置

  • MVC
  • .NET 4.0
  • 默认 SQL 成员身份提供程序(无自定义或覆盖)

例如:

<membership defaultProvider="SqlProvider">
<providers>
<clear />
<add name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="MyDB"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
applicationName="myapp"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="2"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression="" />
</providers>
</membership>
  • 没有自定义或额外键的标准 aspnet_* 数据库表
  • aspnet_Membership_GetAllUsers 从数据库手动运行良好。重定向输出显示正在返回预期结果
  • aspnet_Membership_GetUserByName 也在数据库中运行

异常详情

Specified cast is not valid.
at System.Data.SqlClient.SqlBuffer.get_SqlGuid()
at System.Data.SqlClient.SqlDataReader.GetGuid(Int32 i)
at System.Web.Security.SqlMembershipProvider.GetUser(String username, Boolean userIsOnline)
at System.Web.Security.Membership.GetUser(String username, Boolean userIsOnline)

想法

这几乎就像 Membership.GetUser 方法中有问题。我为我的 System.Web.dll(版本 4.0)反射(reflect)了 System.Web.Security.SqlMembershipProvider.GetUser 的代码,我得到以下信息:

public override MembershipUser GetUser(string username, bool userIsOnline)
{
SecUtility.CheckParameter(ref username, true, false, true, 256, "username");
SqlDataReader reader = (SqlDataReader) null;
try
{
SqlConnectionHolder connectionHolder = (SqlConnectionHolder) null;
try
{
connectionHolder = SqlConnectionHelper.GetConnection(this._sqlConnectionString, true);
this.CheckSchemaVersion(connectionHolder.Connection);
SqlCommand sqlCommand = new SqlCommand("dbo.aspnet_Membership_GetUserByName", connectionHolder.Connection);
sqlCommand.CommandTimeout = this.CommandTimeout;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.Add(this.CreateInputParam("@ApplicationName", SqlDbType.NVarChar, (object) this.ApplicationName));
sqlCommand.Parameters.Add(this.CreateInputParam("@UserName", SqlDbType.NVarChar, (object) username));
sqlCommand.Parameters.Add(this.CreateInputParam("@UpdateLastActivity", SqlDbType.Bit, (object) (bool) (userIsOnline ? 1 : 0)));
sqlCommand.Parameters.Add(this.CreateInputParam("@CurrentTimeUtc", SqlDbType.DateTime, (object) DateTime.UtcNow));
SqlParameter sqlParameter = new SqlParameter("@ReturnValue", SqlDbType.Int);
sqlParameter.Direction = ParameterDirection.ReturnValue;
sqlCommand.Parameters.Add(sqlParameter);
reader = sqlCommand.ExecuteReader();
if (!reader.Read())
return (MembershipUser) null;
string nullableString1 = this.GetNullableString(reader, 0);
string nullableString2 = this.GetNullableString(reader, 1);
string nullableString3 = this.GetNullableString(reader, 2);
bool boolean1 = reader.GetBoolean(3);
DateTime creationDate = reader.GetDateTime(4).ToLocalTime();
DateTime lastLoginDate = reader.GetDateTime(5).ToLocalTime();
DateTime lastActivityDate = reader.GetDateTime(6).ToLocalTime();
DateTime lastPasswordChangedDate = reader.GetDateTime(7).ToLocalTime();
Guid guid = reader.GetGuid(8);
bool boolean2 = reader.GetBoolean(9);
DateTime lastLockoutDate = reader.GetDateTime(10).ToLocalTime();
return new MembershipUser(this.Name, username, (object) guid, nullableString1, nullableString2, nullableString3, boolean1, boolean2, creationDate, lastLoginDate, lastActivityDate, lastPasswordChangedDate, lastLockoutDate);
}
finally
{
if (reader != null)
reader.Close();
if (connectionHolder != null)
connectionHolder.Close();
}
}
catch
{
throw;
}
}

下一步

我希望从 SO 人群那里得到一些关于我下一步应该去哪里的指导。我认为我可以覆盖此方法并将我自己的提供程序放在一起只是为了调试它,或者我可以绕过该死的事情并直接自己调用数据库。这似乎是一些基本的数据库 CRUD 带来的很多心痛。

当前状态

  • 3/5/2013:我今天早上发现我的 aspnet_Users 表有 UserId 作为 nvarchar(256) 而不是唯一标识符。也许 SqlDataReader.GetGuid() 在那里窒息。今晚我将进行一些测试,看看是否是问题所在。我想知道我的表结构是否已过时,因为在线文档将此字段显示为 uniqueidentifier

最佳答案

为什么要使用 membership.CurrentUserName()?您可以只使用 HttpContext.Current.User.Identity.Name;

关于c# - SqlMembershipProvider Membership.GetUser 按名称失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15215963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com