Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET simple membership provider?

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:

enter image description here

When I click register to post I get this error:

enter image description here

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

enter image description here

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

UPDATE

And Exception Snapshot:

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

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.

like image 257
AliRıza Adıyahşi Avatar asked Dec 21 '22 15:12

AliRıza Adıyahşi


1 Answers

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

enter image description here

2) Click collation..

enter image description here

3) And select Latin1_General..

enter image description here

Click ok and save your changes..

like image 132
yusuf Avatar answered Jan 10 '23 10:01

yusuf