在同一个mvc应用程序中使用表单身份验证和Azure AD登录

时间:2017-06-02 09:15:40

标签: asp.net-mvc azure-active-directory form-authentication

我正在使用带有azure AD登录选项(OpenId身份验证)的mvc应用程序。我需要为管理员提供模拟选项。所以我决定使用表单身份验证进行此模拟登录。现在我的问题是在webconfig中启用表单身份验证会影响azure AD登录。一旦添加了表单身份验证,它将与belo url一起显示重定向, http://localhost:9666/members/logon?ReturnUrl=%2fmembers%2flogonusingazure%3freturnUrl%3d%2f&returnUrl=/

用于表单身份验证的Web配置中的代码。

<authentication mode="Forms">
      <forms loginUrl="~/members/logon" cookieless="UseCookies" name="MvcForumAuth" slidingExpiration="true" timeout="432000" />
    </authentication>

谢谢, Nagaraj M。

2 个答案:

答案 0 :(得分:1)

表单身份验证是ASP.NET的身份验证框架的旧版本,最新版本的Visual Studio中ASP.NET MVC的默认模板具有ASP.NET身份框架的实现。

您可以尝试使用OWIN CookieAuthentication和ASP.NET标识来处理使用OpenIdConnect进行AzureAD身份验证的本地身份验证。

要使用asp.net身份,如果使用个人帐户生成新的MVC项目进行身份验证,除了具有直接身份验证和外部登录的登录操作的帐户控制器之外,您还可以看到启动类的自动生成代码(第三方)。

然后,您可以使用ASP.Net OpenID Connect OWIN中间件从Azure Active Directory租户登录用户:

  1. 在NuGet中安装Microsoft.Owin.Security.OpenIdConnect库。
  2. Startup类中,设置OpenIdConnect管道:

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

    现在,您可以使用本地身份帐户和天蓝色广告帐户作为外部用户登录您的应用:enter image description here

  3. 此外,您需要在web.config中设置<authentication mode="none" />,让OWIN从IIS接管您的应用程序的身份验证/授权过程。

答案 1 :(得分:1)

继上述南豫建议之后, 有时应用程序尝试在进入身份登录页面之前执行Azure Ad / OpenId登录。 我无法弄清楚为什么,但配置到表单身份验证似乎工作正常

<authentication mode="Forms">
      <forms loginUrl="~/Account/Login/" timeout="2880"/>
</authentication>