为什么使用 UseCookieAuthentication 会导致异常?
Why using UseCookieAuthentication cause exception?
asp.net 5 mvc 6,visual studio 2015 RC
当我在 (debug/release)
中 运行 应用程序时,为什么这段代码会导致异常
app.UseCookieAuthentication(options =>
{
options.AuthenticationType = "Cookies";
options.LoginPath = new PathString("/Action/Login");
});
System.IO.FileNotFoundException
Could not load file or assembly Microsoft.Framework.Logging.ILoggerFactory, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null or one of their dependencies. Can not find the file specified.
我的 nugets 包:
"Microsoft.Framework.Logging": "1.0.0-beta4",
"Microsoft.Framework.Logging.Console": "1.0.0-beta4",
"Microsoft.AspNet.Diagnostics": "1.0.0-beta4",
"Microsoft.AspNet.Mvc": "6.0.0-beta4",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta4",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta4",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-beta4",
"Microsoft.AspNet.Security.Cookies": "1.0.0-beta3",
"Microsoft.AspNet.Security": "1.0.0-beta3",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
"Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta4",
"Microsoft.Framework.DependencyInjection": "1.0.0-beta4",
"Microsoft.Framework.OptionsModel": "1.0.0-beta4",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta4",
"Npgsql": "2.2.5"
删除安全包并添加:
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta4"
并且,在 login/register 控制器中
var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Username)};
Context.Response.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies")));
beta5 及更高版本:
var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Username)};
await Context.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies")));
asp.net 5 mvc 6,visual studio 2015 RC
当我在 (debug/release)
中 运行 应用程序时,为什么这段代码会导致异常app.UseCookieAuthentication(options =>
{
options.AuthenticationType = "Cookies";
options.LoginPath = new PathString("/Action/Login");
});
System.IO.FileNotFoundException
Could not load file or assembly Microsoft.Framework.Logging.ILoggerFactory, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null or one of their dependencies. Can not find the file specified.
我的 nugets 包:
"Microsoft.Framework.Logging": "1.0.0-beta4",
"Microsoft.Framework.Logging.Console": "1.0.0-beta4",
"Microsoft.AspNet.Diagnostics": "1.0.0-beta4",
"Microsoft.AspNet.Mvc": "6.0.0-beta4",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta4",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta4",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-beta4",
"Microsoft.AspNet.Security.Cookies": "1.0.0-beta3",
"Microsoft.AspNet.Security": "1.0.0-beta3",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
"Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-beta4",
"Microsoft.Framework.DependencyInjection": "1.0.0-beta4",
"Microsoft.Framework.OptionsModel": "1.0.0-beta4",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta4",
"Npgsql": "2.2.5"
删除安全包并添加:
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta4"
并且,在 login/register 控制器中
var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Username)};
Context.Response.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies")));
beta5 及更高版本:
var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Username)};
await Context.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies")));