ASP.NET simple membership provider?

2020-05-24 09:30发布

问题:

I opened Asp.Net Mvc 4 Internet App. template. And I set connection string for my database like this:

<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>

I run project, click register button. I created a new user like this:

When I click register to post I get this error:

But it creates a new user in DB and it does not create membership_user (Membership table is not contain added user info):

I cant find What is the problem. Sometimes there is no error occured.

UPDATE

And Exception Snapshot:

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);
            }
        }
    }
}

LAST EDIT AND SUMMARY

Problem is character ("i"). If I dont use "i" everything is okay. But When I use it I got that error. How Can I fix this problem.

回答1:

Create your database manually (dont allow your project create your database automaticly) and when you create your database set collation to Latin1_General_CI_AS under Options..

OR

If you use Turkish character on your database your database collation has to be Turkish_CI_AS and you can change your column collation.. Follow the steps..

(P.S.: Collation is about sorting if you use Turkish character and if you want to sort like a,b,c,ç,d,e,f,g,h,ı,i,.. your database collation has to be Turkish..)

1) Right click UserName Column and click Modify..

2) Click collation..

3) And select Latin1_General..

Click ok and save your changes..



回答2:

I know its little bit late but, problem is collation definition in your database. If you select "TURKISH_CI_AS" collation while creating database, MembershipProvider is not working as expected.



回答3:

I guess your membership tables are named wrong or have the wrong column names. Check the line similar to

WebSecurity.InitializeDatabaseConnection(
     "StarterSite", "UserProfile", "UserId", "Email", autoCreateTables: true);

in your AppStart.cshtml file.

See this link for more information: http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.initializedatabaseconnection(v=vs.111).aspx