Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebSecurity CreateUserAndAccount_C#_Sql Server 2008_Security_Asp.net Mvc 4 - Fatal编程技术网

C# WebSecurity CreateUserAndAccount

C# WebSecurity CreateUserAndAccount,c#,sql-server-2008,security,asp.net-mvc-4,C#,Sql Server 2008,Security,Asp.net Mvc 4,我正在调用CreateUserAndAccount方法,在用户名和密码中提供字符串。但是我得到一个异常,说硬编码字符串是空的。 这怎么可能!?! 我这里缺什么 代码:WebSecurity.CreateUserAndAccount(“Admin”,“Admin”,null,false) SQLEXCEPTION:无法将值NULL插入到“UserPwd”列、表“servidb.dbo.User”中;列不允许空值。插入失败 我也将表的名称从UserProfile更改为User和Password列。可

我正在调用CreateUserAndAccount方法,在用户名和密码中提供字符串。但是我得到一个异常,说硬编码字符串是空的。 这怎么可能!?! 我这里缺什么

代码:WebSecurity.CreateUserAndAccount(“Admin”,“Admin”,null,false)

SQLEXCEPTION:无法将值NULL插入到“UserPwd”列、表“servidb.dbo.User”中;列不允许空值。插入失败

我也将表的名称从UserProfile更改为User和Password列。可能是这里的错误

问候

附言:我不知道还有什么代码要显示。如果需要更多信息,请询问

编辑:事实上我做到了:

WebSecurity.InitializeDatabaseConnection(“connectionString”、“User”、“UserId”、“UserName”、autoCreateTables:true)

我检查了数据库,表用户是用这个列创建的。我的实体代码是这个

[Table("User")]
public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    [Required]
    public string UserName { get; set; }
    [Required]
    public string UserPwd { get; set; }
}

这可能是因为您更改了这些列名,WebSecurity知道自己要使用哪些列,它实际上使用自己生成的表,除了带有用户名的表之外。如果您使用的是Internet模板,则它可能位于您的初始值SimpleMembership.cs中

WebSecurity.InitializeDatabaseConnection([here database username],
[here database password], [here YOUR talbe name containing user name column], 
[here user name column] , autoCreateTables: true);
因此,您只创建一个带有UserName列的表,仅此而已,其余的表由WebSecurity在新表中创建。

尝试以下操作:

WebSecurity.CreateUserAndAccount("Admin", "Admin", new {UserPwd = ""}, false)

第三个参数表示一个匿名对象,您可以使用它来提供
Required
字段,以便您的SQL获得它需要的所有非空值。我编辑了这篇文章。Resumen,UserPwd列是在UserTable上创建的。您使用的是EF吗?如果是的话,你可能想把autogeneratables改为false。我是的,我想指出这是代码第一。将自动创建设置为false,但仍在进行。是否可以尝试单独创建用户,然后使用
CreateAccount
在memebership表中创建帐户?看看这是否有效。Atm我只是想在mermbership上保存id、用户名和pwd。如果那不起作用,我就试试那个。