我创建了一个新的ASP.NET 5 MVC 6应用程序(工作和学校帐户身份验证),当我转到任何经过身份验证的页面(例如http://localhost:5000/Account/Signin)时,我得到一个重定向循环。如果我发布应用程序并在命令提示符下运行web.cmd(DNX),就会发生这种情况。
我所知道的是,如果我将托管环境设置为开发,则问题在我的开发计算机上得到解决,但是使用相同的“--ASPNET_ENV开发”设置将已发布的应用程序复制到任何其他计算机都会出现重定向问题。 / p>
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel --ASPNET_ENV Development"
},
我可以一贯地重现这一点。关闭身份验证也可以解决问题,但对我没有帮助。发布到Azure也可以。
首先,我想知道为什么将托管环境设置为开发修复了我的开发机器上的重定向问题,其次,我想知道为什么它不适用于任何其他机器。在另一台机器上的IIS中输入也会出现重定向循环问题,但它在我的开发机器上的IIS Express中运行良好。
这很容易重现。在VS 2015中,创建一个新的ASP.NET 5 Web应用程序,选择(工作和学校帐户身份验证)并发布选择文件系统。转到发布目录并运行web.cmd。如果它显示托管环境生产,您可能会遇到问题。将环境更改为开发将解决此问题,但即使托管环境设置为开发,将已发布的应用程序复制到另一台计算机也会出现相同的重定向问题。
更新 我现在知道Azure AD身份验证的设置,例如身份验证:AzureAd:AADInstance位于secrets.json文件%APPDATA%\ microsoft \ UserSecrets \\ secrets.json中。我可以使用命令行user-secret list读取它们。仅当环境设置为“开发”时才会加载此secrets.json文件,以回答我的第一个问题:
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
secrets.json文件也不是部署包的一部分,所以当我将包复制到另一台机器时,它回答了我的第二个问题:为什么它不起作用。
我知道需要弄清楚secretts.json是如何在生产环境中使用的。
Startup.cs按要求提供:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.AspNet.Authentication.OpenIdConnect;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace TestAzureAD
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// 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.AddMvc();
}
// 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)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseCookieAuthentication(options =>
{
options.AutomaticAuthenticate = true;
});
app.UseOpenIdConnectAuthentication(options =>
{
options.AutomaticChallenge = true;
options.ClientId = Configuration["Authentication:AzureAd:ClientId"];
options.Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"];
options.PostLogoutRedirectUri = Configuration["Authentication:AzureAd:PostLogoutRedirectUri"];
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}
答案 0 :(得分:0)
Azure AD身份验证的设置,例如身份验证:AzureAd:AADInstance位于secrets.json文件%APPDATA%\ microsoft \ UserSecrets \ secrets.json中。这也适用于其他身份验证类型。
秘密API可用于管理机密。 https://github.com/aspnet/Home/wiki/DNX-Secret-Configuration
dnu commands install Microsoft.Extensions.SecretManager
user-secret list
在开发机器上进行开发时,可以使用secrets.json文件,但在其他没有secrets.json文件的机器上,需要从appsettings.json和appsettings中读取设置。[environmentname] .json 。将secrets.json设置复制到appsettings.json解决了这个问题,因为azure验证现在知道要去哪里,不再在循环中重定向到自身。
启动代码显示设置是从appsettings.json和appsettings。[environmentname] .json以及开发环境中的secrets.json读取的。
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}