为了账号安全,请及时绑定邮箱和手机立即绑定

ASP.Net MVC 使用文档将默认字符串 ID 更改为和 int 获取错误

ASP.Net MVC 使用文档将默认字符串 ID 更改为和 int 获取错误

C#
蝴蝶刀刀 2022-11-13 13:58:45
我做了一个基本的 ASP.Net MVC Web 项目。我使用 Microsoft 文档将我的所有 ID 从string转换为一个。inthttps://learn.microsoft.com/en-us/aspnet/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity#run我做了这些更改 + MVC Update 3 更改。当我编译并运行我的新项目时,它int在我的文件中显示为一个,但我收到错误并且无法完成运行该项目。这是我的消息在输出中显示的内容:抛出的异常:mscorlib.dll 中的“System.FormatException” mscorlib.dll 中出现“System.FormatException”类型的异常,但未在用户代码中处理 输入字符串的格式不正确。任何输入将不胜感激。我确实运行了迁移,它显示为int但没有在我的浏览器中完全运行。app.UseCookieAuthentication(new CookieAuthenticationOptions            {                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,                LoginPath = new PathString("/Account/Login"),                Provider = new CookieAuthenticationProvider                {                    // Enables the application to validate the security stamp when the user logs in.                    // This is a security feature which is used when you change a password or add an external login to your account.                      OnValidateIdentity = SecurityStampValidator                    .OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(                        validateInterval: TimeSpan.FromMinutes(30),                        regenerateIdentityCallback: (manager, user) =>                         user.GenerateUserIdentityAsync(manager),                         getUserIdCallback:(id)=>Int32.Parse(id.GetUserId()))                }            });我的代码在这部分中断了,那就是异常 id.GetUserId()这是在 startup.auth.cs谢谢你。
查看完整描述

2 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

因此,为了回答您的问题,该Parse方法使用当前语言环境将您的字符串转换为整数。我猜在您的情况下,这GetUserID()将采用您当前语言环境期望逗号作为小数点的格式。因此,提供可选参数CultureInfo.InvariantCulture将使您的区域设置文化不敏感。另一种方法是提供第二个可选参数来提供IFormatProvider

要解决您的错误,请将此行设置为:

(id)=>Int32.Parse(id.GetUserId(),CultureInfo.InvariantCulture)

有关CultureInfo.InvariantCulture的更多信息。


查看完整回答
反对 回复 2022-11-13
?
跃然一笑

TA贡献1826条经验 获得超6个赞

回答我的问题。


在我的 startup.auth.cs


如前所述,我遵循了所有 microsoft 文档。


运行项目时,我不断收到错误消息。


在 CookieAuth 下的 ConfigureAuth() 中添加 CultureInfo 作为第二个参数。


这是我完成的项目代码。


                     {

                    // Enables the application to validate the security stamp when the user logs in.

                    // This is a security feature which is used when you change a password or add an external login to your account.  

                    OnValidateIdentity = SecurityStampValidator

                    .OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>

                    (

                        validateInterval: TimeSpan.FromMinutes(30),

                        regenerateIdentityCallback: (manager, user) => 

                        user.GenerateUserIdentityAsync(manager), 

                        getUserIdCallback:(id) => Int32.Parse(id.GetUserId(), 

                        CultureInfo.InvariantCulture)

                        //getUserIdCallback:(id) => (Int32.Parse(id.GetUserId()))

                    )

                }


查看完整回答
反对 回复 2022-11-13
  • 2 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号