表单和Azure AD身份验证

4
我正在尝试开发一个MVC ASP.NET C#应用程序,它能够支持Azure AD身份验证和表单身份验证。
我已经阅读了相关信息,并得出以下结论:
我有一个登录表单用于表单身份验证,以及一个按钮,可将我重定向到Azure AD登录页面。
在我登录AD之后,它会自动将我重定向到http://localhost/login.aspx?ReturnUrl=%2f
使用以下代码:
Startup.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

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);
        }

我的问题是为什么会重定向到 http://localhost/login.aspx?ReturnUrl=%2f,因为我的应用程序是MVC,我的项目中没有aspx。


你有什么问题吗?我似乎在帖子中找不到它 ;) - rickvdbosch
你好@RickvandenBosch。我的问题是为什么它会重定向到localhost/login.aspx。 - Adrian Vlad
为了确保理解问题正确,我想确认一下,当您创建MVC项目时,表单身份验证是否意味着选择个人用户帐户 - Fei Xue - MSFT
1个回答

1

默认情况下,Forms身份验证的登录URL是Login.aspx。您可以在web.config中指定Windows Forms身份验证的登录URL:

<authentication mode="Forms">
  <forms loginUrl="/account/signin" defaultUrl="/" />
</authentication>

更多信息:FormsAuthentication.LoginUrl属性


感谢你的回答。但是我正在使用表单和 Azure AD 认证。 - Adrian Vlad
这并不意味着您不必为表单身份验证指定登录URL :) 重定向正在发生,您问为什么会重定向到一个.aspx而您没有任何内容。答案是:这是Forms身份验证的默认登录URL。您可能需要使您的AccountController足够聪明,以查看是否已完成Azure身份验证(User.IsAuthenticated?),然后从那里进行重定向。 - rickvdbosch
我已经在我的web.config中设置了这个。问题是来自SignIn,RedirectUrl里面没有字符串。但是当我使用Azure登录后,它会将我重定向到一个新的方法SignedOn(),但我不知道如何检查用户是否已通过Azure AD进行身份验证,因为IsAuthenticated仍然为false。 - Adrian Vlad
你可能想要阅读有关Azure AD身份验证方案的内容。 - rickvdbosch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接