【发布时间】:2020-08-09 02:54:24
【问题描述】:
我使用 Razor Pages 框架创建了一个新的 .net core 3.1.1 Web 应用程序。 创建应用程序时,我将默认身份验证设置为 AzureAd。 当我运行应用程序时,身份验证工作得很好。 生成的 appsettings 文件如下所示:
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "myDomain",
"TenantId": "myTenantId",
"ClientId": "myClientId",
"CallbackPath": "/signin-oidc"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
我在我的应用中创建了一个新控制器,看起来非常简单,就像:
namespace WebApplication1.Controllers
{
public class AccountController : Controller
{
[HttpGet]
public void SignIn()
{
//here comes the logic which checks in what role is the logged User
//the role management stuff will be implemented in the app
}
}
}
这就是我的 Startup.cs 的样子:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
});
services.AddRazorPages().AddMvcOptions(options =>{});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=SignIn}");
});
}
}
我希望能够将 AzureAd/CallbackPath 更改为不同于“/signin-oidc”的内容,例如。我想将其更改为帐户/登录。然后我想从 azure 捕获回调调用,并根据记录的用户电子邮件地址修改令牌以添加一些系统角色并根据用户角色重定向到相应的仪表板页面。管理员和客户可以有不同的仪表板。
所以我尝试更改 "CallbackPath": "/Account/SignIn" 并在 Azure 中更新了 RedirectURI:
然后我再次运行应用程序,在void SignIn() 中设置断点,我再次登录,而不是点击/Account/SignIn,我只是被重定向到主页the https://localhost:44321。
我还尝试在浏览器中手动运行https://localhost:44321/Account/SignIn,我看到了以下错误消息:
An unhandled exception occurred while processing the request.
Exception: OpenIdConnectAuthenticationHandler: message.State is null or empty.
我试图检查documentation 中是否有任何东西,但我没有发现任何有用的东西。 关于我应该怎么做才能让它发挥作用的任何想法? 干杯
编辑:
我也使用Microsoft.AspNetCore.Authentication.AzureAD.UI 框架。
【问题讨论】:
标签: c# authentication asp.net-core azure-active-directory asp.net-core-3.1