简体   繁体   English

ASP.NET MVC中的成员身份和授权

[英]Membership & Authorization in ASP.NET MVC

I'm writing a asp.net mvc3 project with bundle of SQL Server 2012 and EF. 我正在编写一个带有SQL Server 2012和EF捆绑的asp.net mvc3项目。 At this moment I need to add authorization to it. 此时我需要添加授权。 I Found one project on codplex http://codefirstmembership.codeplex.com/ and added it to my project. 我在codplex http://codefirstmembership.codeplex.com/上找到了一个项目,并将其添加到我的项目中。 Created table in database 在数据库中创建表

CREATE TABLE [dbo].[Users](
[UserId] [uniqueidentifier] NOT NULL,
[Username] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
[Password] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Comment] [nvarchar](50) NULL,
[IsApproved] [bit] NULL,
[PasswordFailuresSinceLastSuccess] [int] NULL,
[LastPasswordFailureDate] [datetime] NULL,
[LastActivityDate] [datetime] NULL,
[LastLockoutDate] [datetime] NULL,
[LastLoginDate] [datetime] NULL,
[ConfirmationToken] [nvarchar](50) NULL,
[CreateDate] [datetime] NULL,
[IsLockedOut] [bit] NULL,
[LastPasswordChangedDate] [datetime] NULL,
[PasswordVerificationToken] [nvarchar](50) NULL,
[PasswordVerificationTokenExpirationDate] [datetime] NULL
) ON [PRIMARY]

and when I try to use CreateUser method it was get me an error, like next: 当我尝试使用CreateUser方法时,它给我一个错误,就像下一个:

String or binary data would be truncated. The statement has been
terminated.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: String or
binary data would be truncated. The statement has been terminated.

It's error from the CodeFirstMembershipProvider.cs on the line Context.SaveChanges(); 来自Context.SaveChanges()行的CodeFirstMembershipProvider.cs的错误;

I checked google and it told me that I have differences between model and table in my database, but I don't see any mistakes. 我检查了谷歌,它告诉我,我的数据库中的模型和表之间存在差异,但我没有看到任何错误。 Any ideas? 有任何想法吗?

** UPDATE** This is my model class **更新**这是我的模特课

public class User {
    [Key]
    public virtual Guid UserId { get; set; }

    [Required]
    public virtual String Username { get; set; }

    [Required]
    public virtual String Email { get; set; }

    [Required, DataType(DataType.Password)]
    public virtual String Password { get; set; }

    public virtual String FirstName { get; set; }
    public virtual String LastName { get; set; }

    [DataType(DataType.MultilineText)]
    public virtual String Comment { get; set; }

    public virtual Boolean IsApproved { get; set; }
    public virtual int PasswordFailuresSinceLastSuccess { get; set; }
    public virtual DateTime? LastPasswordFailureDate { get; set; }
    public virtual DateTime? LastActivityDate { get; set; }
    public virtual DateTime? LastLockoutDate { get; set; }
    public virtual DateTime? LastLoginDate { get; set; }
    public virtual String ConfirmationToken { get; set; }
    public virtual DateTime? CreateDate { get; set; }
    public virtual Boolean IsLockedOut { get; set; }
    public virtual DateTime? LastPasswordChangedDate { get; set; }
    public virtual String PasswordVerificationToken { get; set; }
    public virtual DateTime? PasswordVerificationTokenExpirationDate { get; set; }

    public virtual ICollection<Role> Roles { get; set; }
}

It means you're trying to put too much information into a field in your table. 这意味着您正在尝试将过多信息放入表中的字段中。 For example, if you try to create a user with a username 51 characters long: 例如,如果您尝试使用长度为51个字符的用户名创建用户:

"123456789012345678901234567890123456789012345678901"

This will throw a String or binary data would be truncated error because the definition of the Username row only allows for 50 characters: 这会抛出一个String or binary data would be truncated错误,因为Username行的定义只允许50个字符:

[Username] [nvarchar](50)

You need to find what data you are passing into CreateUser() which is breaking the rules set out by your CREATE TABLE statement. 您需要找到传递给CreateUser() ,这违反了CREATE TABLE语句设置的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM