OWIN / Identity 2.0 - 将pk从字符串更改为GUID

时间:2014-07-30 23:10:11

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

我试图从数据库nvarchar(128)改变asp.net身份的pk系统 - > uniqueidentifier和来自字符串的代码 - > GUID。根据这个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()))
            }
        });  

我得到两个我无法理解的错误。身份的结构使我与许多仿制药混淆了。我理解它说接收错误的参数类型,但我不知道如何解决这个问题。

错误

  

错误1最佳重载方法匹配   “Microsoft.AspNet.Identity.Owin.SecurityStampValidator.OnValidateIdentity(System.TimeSpan,   System.Func&gt;中   System.Func)'有   一些无效的论点

     

错误2参数2:无法从'lambda表达式'转换为   'System.Func&GT;'

任何人都可以提供一些见解吗?

1 个答案:

答案 0 :(得分:11)

  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())))

            }
        });