身份服务器:在 Angular 中登录和注销后重定向?
Identity Server: Redirect after login and logout in Angular?
我在一个基于.NET Core 的Angular 项目中使用IdentityServer 并通过修改它来使用Identity Server razor 登录页面。除此登录页面外,我使用 Angular 页面并使用 Angular 中的延迟加载功能重定向相关页面。
但是,登录和注销后我需要一些不同的场景,如下所述:
1. 用户登录后,我想重定向一个特定的页面。我该如何设置这个页面?在 IdentityServer 操作方法中,还是在登录按钮的 onClick 操作中?我还可以通过 hperlink 使用 href 吗?
2. 我是否应该使用 ActionLink
等,因为登录页面是通过将页面重定向到控制器然后打开 Angular页?还是使用 href 或 routerlink 属性 作为登录按钮并轻松调用相关 Angular 路由更好?
3. 对于注销,我想重定向一个 Angular 页面。我应该在 IdentityServer 配置的注销或注销回调属性中设置它吗?
如有任何帮助,我们将不胜感激。
更新: 这是可用于重定向或回调设置的常量文件:
export const LogoutActions = {
LogoutCallback: 'logout-callback',
Logout: 'logout',
LoggedOut: 'logged-out'
};
export const LoginActions = {
Login: 'login',
LoginCallback: 'login-callback',
LoginFailed: 'login-failed',
Profile: 'profile',
Register: 'register'
};
let applicationPaths: ApplicationPathsType = {
DefaultLoginRedirectPath: '/',
ApiAuthorizationClientConfigurationUrl: `/_configuration/${ApplicationName}`,
Login: `authentication/${LoginActions.Login}`,
LoginFailed: `authentication/${LoginActions.LoginFailed}`,
LoginCallback: `authentication/${LoginActions.LoginCallback}`,
Register: `authentication/${LoginActions.Register}`,
Profile: `authentication/${LoginActions.Profile}`,
LogOut: `authentication/${LogoutActions.Logout}`,
LoggedOut: `authentication/${LogoutActions.LoggedOut}`,
LogOutCallback: `authentication/${LogoutActions.LogoutCallback}`,
LoginPathComponents: [],
LoginFailedPathComponents: [],
LoginCallbackPathComponents: [],
RegisterPathComponents: [],
ProfilePathComponents: [],
LogOutPathComponents: [],
LoggedOutPathComponents: [],
LogOutCallbackPathComponents: [],
IdentityRegisterPath: '/Identity/Account/Register',
IdentityManagePath: '/Identity/Account/Manage'
};
applicationPaths = {
...applicationPaths,
LoginPathComponents: applicationPaths.Login.split('/'),
LoginFailedPathComponents: applicationPaths.LoginFailed.split('/'),
RegisterPathComponents: applicationPaths.Register.split('/'),
ProfilePathComponents: applicationPaths.Profile.split('/'),
LogOutPathComponents: applicationPaths.LogOut.split('/'),
LoggedOutPathComponents: applicationPaths.LoggedOut.split('/'),
LogOutCallbackPathComponents: applicationPaths.LogOutCallback.split('/')
};
您要做的是将您的 IdentityServer 客户端配置设置为通过 RedirectUris
和 PostLogoutRedirectUris
指向那些不同的组件。然后在 Angular 应用程序中,当您配置 UserManager
实例时,设置 redirect_uri
和 post_logout_redirect_uri
您希望身份服务器在 Login/Logout 之后重定向到的位置方法齐全。这些配置需要在客户端和服务器之间匹配,否则 IdentityServer 将拒绝请求。 Brock Allen 为 Angular 开发的示例应用程序阐明了这一点,Angular oidc-client.js。
配置完成后,重定向应该是自动的,您不需要修改 IdentityServer 中的重定向。这仅涵盖 Login/Logout 流程。我看到您在 Angular 应用程序中有注册和失败的登录定义。您可以做的是修改视图控制器以通过 return Redirect("some\url\to\angular\component");
重定向用户以将用户发送到该组件。
我在一个基于.NET Core 的Angular 项目中使用IdentityServer 并通过修改它来使用Identity Server razor 登录页面。除此登录页面外,我使用 Angular 页面并使用 Angular 中的延迟加载功能重定向相关页面。
但是,登录和注销后我需要一些不同的场景,如下所述:
1. 用户登录后,我想重定向一个特定的页面。我该如何设置这个页面?在 IdentityServer 操作方法中,还是在登录按钮的 onClick 操作中?我还可以通过 hperlink 使用 href 吗?
2. 我是否应该使用 ActionLink
等,因为登录页面是通过将页面重定向到控制器然后打开 Angular页?还是使用 href 或 routerlink 属性 作为登录按钮并轻松调用相关 Angular 路由更好?
3. 对于注销,我想重定向一个 Angular 页面。我应该在 IdentityServer 配置的注销或注销回调属性中设置它吗?
如有任何帮助,我们将不胜感激。
更新: 这是可用于重定向或回调设置的常量文件:
export const LogoutActions = {
LogoutCallback: 'logout-callback',
Logout: 'logout',
LoggedOut: 'logged-out'
};
export const LoginActions = {
Login: 'login',
LoginCallback: 'login-callback',
LoginFailed: 'login-failed',
Profile: 'profile',
Register: 'register'
};
let applicationPaths: ApplicationPathsType = {
DefaultLoginRedirectPath: '/',
ApiAuthorizationClientConfigurationUrl: `/_configuration/${ApplicationName}`,
Login: `authentication/${LoginActions.Login}`,
LoginFailed: `authentication/${LoginActions.LoginFailed}`,
LoginCallback: `authentication/${LoginActions.LoginCallback}`,
Register: `authentication/${LoginActions.Register}`,
Profile: `authentication/${LoginActions.Profile}`,
LogOut: `authentication/${LogoutActions.Logout}`,
LoggedOut: `authentication/${LogoutActions.LoggedOut}`,
LogOutCallback: `authentication/${LogoutActions.LogoutCallback}`,
LoginPathComponents: [],
LoginFailedPathComponents: [],
LoginCallbackPathComponents: [],
RegisterPathComponents: [],
ProfilePathComponents: [],
LogOutPathComponents: [],
LoggedOutPathComponents: [],
LogOutCallbackPathComponents: [],
IdentityRegisterPath: '/Identity/Account/Register',
IdentityManagePath: '/Identity/Account/Manage'
};
applicationPaths = {
...applicationPaths,
LoginPathComponents: applicationPaths.Login.split('/'),
LoginFailedPathComponents: applicationPaths.LoginFailed.split('/'),
RegisterPathComponents: applicationPaths.Register.split('/'),
ProfilePathComponents: applicationPaths.Profile.split('/'),
LogOutPathComponents: applicationPaths.LogOut.split('/'),
LoggedOutPathComponents: applicationPaths.LoggedOut.split('/'),
LogOutCallbackPathComponents: applicationPaths.LogOutCallback.split('/')
};
您要做的是将您的 IdentityServer 客户端配置设置为通过 RedirectUris
和 PostLogoutRedirectUris
指向那些不同的组件。然后在 Angular 应用程序中,当您配置 UserManager
实例时,设置 redirect_uri
和 post_logout_redirect_uri
您希望身份服务器在 Login/Logout 之后重定向到的位置方法齐全。这些配置需要在客户端和服务器之间匹配,否则 IdentityServer 将拒绝请求。 Brock Allen 为 Angular 开发的示例应用程序阐明了这一点,Angular oidc-client.js。
配置完成后,重定向应该是自动的,您不需要修改 IdentityServer 中的重定向。这仅涵盖 Login/Logout 流程。我看到您在 Angular 应用程序中有注册和失败的登录定义。您可以做的是修改视图控制器以通过 return Redirect("some\url\to\angular\component");
重定向用户以将用户发送到该组件。