ASP.NET SPA模板中ASP.NET身份代码更改的原因?

时间:2014-12-11 16:13:40

标签: asp.net asp.net-mvc asp.net-web-api asp.net-identity asp.net-identity-2

对于ASP.NET SPA应用程序,VS2013附带的代码模板与VS2013更新2/3/4附带的代码模板非常不同。作为参考,这里是来自

的相同模板

VS2013:https://www.dropbox.com/s/nc9t691adg2q9ac/OldTemplate.zip?dl=0

VS2013更新4:https://www.dropbox.com/s/sk8qajyxy4kx4m3/NewTemplate.zip?dl=0

最大的区别似乎是使用原始模板登录是在没有回发的情况下完成的,完全通过JavaScript(这似乎与SPA精神保持一致)。但是在模板的更高版本中,在登录期间会发生完整的回发。有许多其他更改似乎与此主要更改有关,例如,在旧版本中,ApplicationOAuthProvider.cs中存在此方法:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        using (UserManager<IdentityUser> userManager = _userManagerFactory())
        {
            IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
                context.Options.AuthenticationType);
            ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                CookieAuthenticationDefaults.AuthenticationType);
            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
    }

此方法在较新的模板中完全省略。

对于为何进行此类重大更改表示理解。我没有看到任何优势,对于SPA风格的应用程序,我更喜欢缺乏回发。但也许考虑了重要的安全或设计原因,我也应该考虑。

...谢谢 -ben

1 个答案:

答案 0 :(得分:0)

我的猜测是,这是一种疏忽或缺乏时间。

如果这不是最充满代码示例的答案,我很抱歉......但当您执行File > New Project时,SPA模板并不是最常用的方案。

我可以理解为什么它不会像WebApplication那样多。

如果您对模板感兴趣,我建议您查看SideWaffle,其中包含大量可以帮助您进行自举的模板。