[英]Forms and Azure AD Authentication
I am trying to develop a MVC ASP.NET C# Application that can support both Azure AD Authentication and Forms authentication.我正在尝试开发一个 MVC ASP.NET C# 应用程序,它可以同时支持 Azure AD 身份验证和 Z645024253188A283E 身份验证。
I've read about it and came to the following conclusion:我已经阅读并得出以下结论:
I have a login-form for Forms Auth and a button which redirects me to Azure AD Login.我有一个 Forms Auth 的登录表单和一个将我重定向到 Azure AD 登录的按钮。
After I login in AD, it auto redirects me to http://localhost/login.aspx?ReturnUrl=%2f .在我登录 AD 后,它会自动将我重定向到http://localhost/login.aspx?ReturnUrl=%2f 。
Using following code:使用以下代码:
Startup.cs启动.cs
public partial class Startup
{
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
RedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
}
}
});
}
}
AccountController.cs AccountController.cs
public void SignIn()
{
// Send an OpenID Connect sign-in request.
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
public void SignOut()
{
// Send an OpenID Connect sign-out request.
HttpContext.GetOwinContext().Authentication.SignOut(
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
public void EndSession()
{
// If AAD sends a single sign-out message to the app, end the user's session, but don't redirect to AAD for sign out.
HttpContext.GetOwinContext().Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationType);
}
My question is why does it redirects me to http://localhost/login.aspx?ReturnUrl=%2f , giving that the app is a MVC and there is no aspx in my project.我的问题是为什么它会将我重定向到http://localhost/login.aspx?ReturnUrl=%2f ,因为该应用程序是 MVC 并且我的项目中没有 aspx。
By default, the login URL for Forms authentication is Login.aspx. 默认情况下,表单身份验证的登录URL为Login.aspx。 You can specify the login URL for Windows Forms authentication in the web.config:
您可以在web.config中指定Windows窗体身份验证的登录URL:
<authentication mode="Forms">
<forms loginUrl="/account/signin" defaultUrl="/" />
</authentication>
More info: FormsAuthentication.LoginUrl Property 更多信息: FormsAuthentication.LoginUrl属性
Have you found a solution to this?您找到解决方案了吗? I'm having the same problem with a Legacy Project.
我在旧项目中遇到了同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.