如何更改 Microsoft 身份验证提供程序登录的默认回调?
How to change default callback of the Microsoft authentication provider login?
在我的 ASP.Net 核心应用程序中,我实现了 Microsoft 外部登录。我现在希望覆盖默认的登录回调,如果在本地主机和该端口上当然 运行,文档将其列为 https://localhost:5001/signin-microsoft
。 here 上的说明然后声明回调覆盖将是这样的:https://contoso.azurewebsites.net/.auth/login/microsoftaccount/callback
。
我对回调的实施位置感到有点困惑。目前,我在基本控制器 class 中实现了 ExternalLoginCallback()
回调方法。但是从上面的例子来看,它看起来不应该是控制器的一部分。
回调应该在 Startup.cs
、控制器或我目前不知道的其他文件中吗?
The instructions on here then state that the callback override would be something like this: https://contoso.azurewebsites.net/.auth/login/microsoftaccount/callback.
这与 Azure 应用服务中的 built-in authentication and authorization support 有关。您是否在 Azure App 服务中托管您的应用程序?
如果是:
如果您启用了应用服务的 Authentication and authorization
功能,这意味着您正在使用 Azure 中的 built-in authentication and authorization support。该功能将接管您的应用程序的身份验证和授权,这意味着即使您删除了应用程序中的外部 Azure AD 身份验证代码,身份验证和授权仍然有效。那么你可以:
使用Authentication and authorization
应用服务的特性,删除Owin微软账号认证中间件相关代码。
禁用应用服务的Authentication and authorization
功能,使用Microsoft Account external login(Microsoft.AspNetCore.Authentication.MicrosoftAccount包)。
如果没有:
那么您应该遵循文档:Microsoft Account external login。您可以通过 :
配置回调 url
microsoftOptions.CallbackPath = "/home/about";
但是如果您使用 ASP.NET 身份模板和 Microsoft 帐户外部登录。 Microsoft 验证后,asp.net 将检查用户身份是否存在于数据库中。由于 ASP.NET Core 2.1 及更高版本提供 ASP.NET Core Identity 作为 Razor Class 库。如果你想在身份验证后将用户重定向到另一个页面,你可以:
ASP.NET 核心项目中的脚手架标识:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=visual-studio
之后,修改Areas.Identity.Pages.Account.Login.cshtml.cs
中的重定向url:
public IActionResult OnPost(string provider, string returnUrl = null)
{
returnUrl = "/home/contact";
// Request a redirect to the external login provider.
var redirectUrl = Url.Page("./ExternalLogin", pageHandler: "Callback", values: new { returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return new ChallengeResult(provider, properties);
}
在我的 ASP.Net 核心应用程序中,我实现了 Microsoft 外部登录。我现在希望覆盖默认的登录回调,如果在本地主机和该端口上当然 运行,文档将其列为 https://localhost:5001/signin-microsoft
。 here 上的说明然后声明回调覆盖将是这样的:https://contoso.azurewebsites.net/.auth/login/microsoftaccount/callback
。
我对回调的实施位置感到有点困惑。目前,我在基本控制器 class 中实现了 ExternalLoginCallback()
回调方法。但是从上面的例子来看,它看起来不应该是控制器的一部分。
回调应该在 Startup.cs
、控制器或我目前不知道的其他文件中吗?
The instructions on here then state that the callback override would be something like this: https://contoso.azurewebsites.net/.auth/login/microsoftaccount/callback.
这与 Azure 应用服务中的 built-in authentication and authorization support 有关。您是否在 Azure App 服务中托管您的应用程序?
如果是:
如果您启用了应用服务的 Authentication and authorization
功能,这意味着您正在使用 Azure 中的 built-in authentication and authorization support。该功能将接管您的应用程序的身份验证和授权,这意味着即使您删除了应用程序中的外部 Azure AD 身份验证代码,身份验证和授权仍然有效。那么你可以:
使用
Authentication and authorization
应用服务的特性,删除Owin微软账号认证中间件相关代码。禁用应用服务的
Authentication and authorization
功能,使用Microsoft Account external login(Microsoft.AspNetCore.Authentication.MicrosoftAccount包)。
如果没有:
那么您应该遵循文档:Microsoft Account external login。您可以通过 :
配置回调 urlmicrosoftOptions.CallbackPath = "/home/about";
但是如果您使用 ASP.NET 身份模板和 Microsoft 帐户外部登录。 Microsoft 验证后,asp.net 将检查用户身份是否存在于数据库中。由于 ASP.NET Core 2.1 及更高版本提供 ASP.NET Core Identity 作为 Razor Class 库。如果你想在身份验证后将用户重定向到另一个页面,你可以:
ASP.NET 核心项目中的脚手架标识:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=visual-studio
之后,修改
Areas.Identity.Pages.Account.Login.cshtml.cs
中的重定向url:public IActionResult OnPost(string provider, string returnUrl = null) { returnUrl = "/home/contact"; // Request a redirect to the external login provider. var redirectUrl = Url.Page("./ExternalLogin", pageHandler: "Callback", values: new { returnUrl }); var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); return new ChallengeResult(provider, properties); }