繁体   English   中英

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

[英]Using form authentication and Azure AD login in same mvc application

我正在使用带有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.

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

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

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

然后,您可以使用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); } } }); 

    现在,您可以使用本地身份帐户和Azure广告帐户作为外部用户登录您的应用程序: 在此处输入图片说明

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

按照上述Nan Yu的建议,有时应用程序会尝试先进入Azure Ad / OpenId登录,然后再进入身份登录页面。 我不知道为什么,但是配置表单身份验证似乎可以正常工作

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM