【问题标题】:OWIN/Identity 2.0 - Changing pk from string to GUIDOWIN/Identity 2.0 - 将 pk 从字符串更改为 GUID
【发布时间】:2014-07-30 23:10:10
【问题描述】:

我正在尝试从数据库 nvarchar(128) -> uniqueidentifier 和字符串 -> Guid 的代码中更改 asp.net 身份的 pk 系统。在此article 之后,基于将 pk 更改为 int32 我只有一个问题,我似乎无法解决。

在我的Startup.Auth.cs 课程中,我更改了以下内容

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {   //error on the line below
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(TimeSpan.FromMinutes(20), (manager, user) => user.GenerateUserIdentityAsync(manager), (identity) => Guid.Parse(identity.GetUserId()))
            }
        });  

我遇到了两个我无法理解的错误。 Identity 的结构让我对这么多泛型感到困惑。我知道它说它接收到错误的参数类型,但我不知道如何解决这个问题。

错误

错误 1 ​​最好的重载方法匹配 'Microsoft.AspNet.Identity.Owin.SecurityStampValidator.OnValidateIdentity(System.TimeSpan, System.Func>, System.Func)' 有 一些无效的参数

错误 2 参数 2:无法从“lambda 表达式”转换为 'System.Func>'

谁能提供一点见解?

【问题讨论】:

标签: c# asp.net asp.net-mvc asp.net-identity


【解决方案1】:
  app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/_layouts/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, WebUser,Guid>(
                    validateInterval: TimeSpan.FromMinutes(30),
                     regenerateIdentityCallback: (manager, user) => 
                    user.GenerateUserIdentityAsync(manager), 
                getUserIdCallback:(id)=>(Guid.Parse(id.GetUserId())))

            }
        });        

【讨论】:

  • 那是很多带有描述的代码。描述为什么代码回答问题总是一个好主意。请edit您的答案添加描述。
  • 如果我使用这种方法,我会得到异常“找不到用户 ID”。注册后。由 GenerateUserIdentityAsync -> manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 抛出有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2015-08-09
  • 1970-01-01
  • 2013-11-17
相关资源
最近更新 更多