ASP.NET简单成员资格提供者是什么?

4
我打开了Asp.Net Mvc 4 Internet App模板,并为我的数据库设置了连接字符串,如下所示:

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=ALI-PC\;Initial Catalog=OsosPlusDb;Persist Security Info=True;User ID=sa;Password=sa;" providerName="System.Data.SqlClient" />
</connectionStrings>

我运行项目,点击注册按钮。我像这样创建了一个新用户:

enter image description here

当我点击注册以发布时,我会得到这个错误:

enter image description here

但它确实在数据库中创建了一个新用户,并且没有创建 membership_user (Membership 表不包含添加的用户信息):

enter image description here

我找不到问题所在。有时候不会出现错误。

更新

异常快照如下:

enter image description here

AcoountModel:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
}

InitializeSimpleMembershipAttribute.cs

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
    private static SimpleMembershipInitializer _initializer;
    private static object _initializerLock = new object();
    private static bool _isInitialized;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Ensure ASP.NET Simple Membership is initialized only once per app start
        LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
    }

    private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            Database.SetInitializer<UsersContext>(null);

            try
            {
                using (var context = new UsersContext())
                {
                    if (!context.Database.Exists())
                    {
                        // Create the SimpleMembership database without Entity Framework migration schema
                        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                    }
                }

                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
            }
        }
    }
}

最终编辑和总结

问题出在字符“i”上。如果我不使用“i”,一切都正常。但是当我使用它时,就会出现错误。我该如何解决这个问题。


你应该在生成错误信息的地方设置一个断点。 很可能有一个内部异常,可以给你更多的线索。 - nograde
内部异常为空。 - AliRıza Adıyahşi
3个回答

8
手动创建数据库(不要让项目自动创建数据库),在创建数据库时,在选项下将排序规则设置为Latin1_General_CI_AS。
或者,如果您的数据库中使用了土耳其字符,则您的数据库排序规则必须为Turkish_CI_AS,并且您可以更改列排序规则。按照以下步骤操作:
1)右键单击UserName列,然后单击修改。
2)单击排序规则。
3)选择Latin1_General。
单击确定并保存更改。
(注:排序规则是关于排序的,如果您使用土耳其字符并希望按如下排序:a、b、c、ç、d、e、f、g、h、ı、i... 您的数据库排序规则必须为Turkish。)

如果数据库和列排序规则为SQL_Latin1_General_CP1_CI_AS,是否应该替换为Windows排序规则Latin1_General? - Ravi Rajindu

2

我知道可能有点晚了,但问题出在你的数据库的排序规则定义上。如果在创建数据库时选择了"TURKISH_CI_AS"作为排序规则,MembershipProvider的功能将无法正常工作。


1

但是,它有时候可以工作。我无法理解这个。这是我的初始化代码 WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); - AliRıza Adıyahşi
看看这个帖子:http://forums.asp.net/t/1715988.aspx/1 ,也许你在 SQL Server 的排序规则上遇到了问题? - Jan
如果有错,为什么它会将用户添加到UserProfile表并返回错误。 - AliRıza Adıyahşi
我测试过了。我认为你是对的。问题在于字符。但是我该如何修复它呢? - AliRıza Adıyahşi
1
哪些字符给你带来了错误?你的本地机器和 SQL Server 的区域设置/排序规则是什么? - Jan
"i" 是问题。(排序规则为 Turkish_CI_AS)。为什么用户添加了 UserProfile 表而没有添加 Membership 表?难道也不应该添加 UserProfile 表吗? - AliRıza Adıyahşi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接