简体   繁体   English

ApplicationSignInManager类抛出无效的强制转换异常

[英]ApplicationSignInManager class throws invalid cast exception

This is the code taken from the MVC5 account controller class ( this is created as a template when you set up a asp.net project) However when i pass in the following parameter to this line of code 这是从MVC5帐户控制器类中获取的代码(当您设置asp.net项目时,这是作为模板创建的)但是当我将以下参数传递给此行代码时

model.UserName="User1", model.password="Password1",model.RememberMe=false, shouldLockout=false model.UserName =“User1”,model.password =“Password1”,model.RememberMe = false,shouldLockout = false

 var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false); 

it throws an exception saying 它引发了一个例外

Unable to cast object of type 'System.Int32' to type 'System.String'. 无法将类型为“System.Int32”的对象强制转换为“System.String”。

What i don't understand is why does the line of code above thinks the parameter is int when all the parameter are either string or boolean. 我不明白的是,当所有参数都是字符串或布尔值时,为什么上面的代码行认为参数是int。

Anyone up for help? 有人帮忙吗? Thanks for advance. 谢谢你提前。

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);   

    }

Code for LoginViewModel Class LoginViewModel类的代码

public class LoginViewModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

In attempting to resolve a very similar error (Unable to cast object of type system.guid to type system.string) for the same scenario, I traced the problem to the data types that had been used for the key columns of two Roles tables (AspNetRoles and AspNetUserRoles). 在尝试解决同一场景中非常类似的错误(无法将system.guid类型的对象转换为类型system.string)时,我将问题追溯到已用于两个Roles表的键列的数据类型( AspNetRoles和AspNetUserRoles)。 These needed to be of type nvarchar , not uniqueidentifier. 这些必须是nvarchar类型,而不是uniqueidentifier。

The SQL query behind PasswordSignInAsync gathers data from AspNetUsers, AspNetUserRoles, AspNetUserClaims and AspNetUserLogins so I suggest you check that your key column types across all of these tables match what the expected entities that this data will be mapped to. PasswordSignInAsync背后的SQL查询从AspNetUsers,AspNetUserRoles,AspNetUserClaims和AspNetUserLogins收集数据,因此我建议您检查所有这些表中的键列类型是否与此数据将映射到的预期实体相匹配。 As far as I'm aware, these should be as follows: 据我所知,这些应该如下:

AspNetUsers > [Id] NVARCHAR (128) AspNetUsers> [Id] NVARCHAR(128)

AspNetRoles > [Id] NVARCHAR (128) AspNetRoles> [Id] NVARCHAR(128)

AspNetUserRoles > [UserId] NVARCHAR (128), [RoleId] NVARCHAR (128) AspNetUserRoles> [UserId] NVARCHAR(128),[RoleId] NVARCHAR(128)

AspNetUserClaims > [Id] INT AspNetUserClaims> [Id] INT

AspNetUserLogins > [UserId] NVARCHAR (128) AspNetUserLogins> [UserId] NVARCHAR(128)

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

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