当我请求装饰为[授权]的控制器操作而不是重定向到登录页面时,我会收到401错误.
When I request a controller action that is [Authorize] decorated instead of being redirected to the login page I receive a 401 error.
这是一个使用IIS Express上运行的身份模板的核心mvc应用程序.
This is a core mvc app using the identity template running on IIS express.
当我从program.cs运行该应用程序时,重定向到登录的工作正常. 我已经为cookie身份验证添加了明确的指导,以使/Account/Login重定向同时用于配置和服务"部分,以及配置Identity来执行此重定向.
When i run the app from program.cs the redirect to login works fine. I've added explicit directions to for the cookie authentication to use the /Account/Login redirect both for configuration and services section, as well as configuring Identity to perform this redirect.
我无法正常工作.下面是我的StartUp类,我应该进行哪些更改以使其在IIS express中正常工作?:
I can't get it to work. Below is my StartUp class, what should I change to make it work in IIS express?:
public class Startup { private MapperConfiguration _mapperConfiguration { get; set; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); if (env.IsDevelopment()) { // For more details on using the user secret store see go.microsoft/fwlink/?LinkID=532709 builder.AddUserSecrets(); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); _mapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfile(new AutoMapperProfileConfiguration()); }); } public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>( option => { option.Cookies.ApplicationCookie.LoginPath = "/Account/Login"; option.Cookies.ApplicationCookie.AutomaticChallenge = true; option.Cookies.ApplicationCookie.AutomaticAuthenticate = true; }) .AddEntityFrameworkStores<ApplicationDbContext>(); services.AddDataProtection(); services.AddMvc(); services.AddSignalR(); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); services.Configure<AuthMessageSenderOptions>(Configuration); services.Configure<IISOptions>(options => options.AutomaticAuthentication = true); services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context, RoleManager<IdentityRole> roleManager) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseStaticFiles(); app.UseIdentity(); // Add external authentication middleware below. To configure them please see go.microsoft/fwlink/?LinkID=532715 //app.UseStatusCodePagesWithReExecute("/Home/Error/{0}"); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "MyCookies", SlidingExpiration = true, AutomaticAuthenticate = true, AutomaticChallenge = true, LoginPath = new PathString("/Account/Login") }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); routes.MapRoute( name: "index", template: "{controller=Home}/{id?}", defaults: new { action = "Index" }); }); app.UseSignalR(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } MyDbInit.Init(context, roleManager); } }推荐答案
我整夜都遇到了同样的问题,无法找到解决方案.直接从Kestrel运行站点可以很好地重定向,但是通过IIS或IIS Express,它根本不会重定向-会进入白页.
I had this same problem all night and could not find a solution to it. Running the site directly from Kestrel redirected fine, but through IIS or IIS Express it simply would not redirect - it would go to a white page.
将其发布到Identity Git后,我意识到我的模板已设置为在框架的1.0.1(而不是1.1.0)下运行.我将其更新为使用1.1.0,并将所有Nuget程序包更新为1.1.0,现在它已在IIS和IIS Express中正确重定向.
After posting to the Identity Git about it, I realized that my template was set up to run under 1.0.1 of the framework, not 1.1.0. I updated it to use 1.1.0 and updated all the Nuget packages to 1.1.0 and now it is redirecting in IIS and IIS Express properly.
我不确定软件包是否更新了修复"了一些棘手的内容,或者这仅仅是1.0.1的问题,而该问题已在1.1.0中修复.
I'm not sure if the package updates "fixed" something that was screwy, or if this was simply a problem with 1.0.1 that was fixed in 1.1.0.
blogs.msdn.microsoft/webdev/2016/11/16/announcing-asp-net-core-1-1/
更多推荐
无法获得.net核心MVC将401重定向到/Account/Login
发布评论