最简单的方法是只为身份验证/授权创建2个不同的演示项目。
这样做的好处在于依赖现有框架和标准配置。
然后,您可以决定要么
- 为每个互联网用户创建AD用户,或
- 为每个AD用户创建DB / Internet用户。
为每个AD用户创建Identity用户更容易进一步实现。然后,相同的cookie和过滤器可以存在于整个应用程序中。
在这种情况下,您可以选择要么
- 使用子域名来运行您的应用程序
- AD身份验证项目可以具有身份验证/授权的唯一目的,然后Web应用程序可以表示您的应用程序的其余部分。
或者,如果您想要一个真正统一的解决方案,请使用MohammadYounes/Owin-MixedAuth
MohammadYounes/Owin-MixedAuth
Install-Package OWIN-MixedAuth
在Web.config文件中:
<location path="MixedAuth">
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</location>
在 Startup.Auth.cs 文件中
app.UseMixedAuth(cookieOptions);
:
工作原理:
处理程序使用ApplyResponseChallengeAsync
确认请求是401挑战。如果是,则重定向到回调路径,从IIS请求身份验证,IIS已配置为查询AD。
AuthenticationResponseChallenge challenge = Helper.LookupChallenge(
Options.AuthenticationType, Options.AuthenticationMode)
未经授权的用户试图使用需要身份验证的资源导致了401挑战。
处理程序使用InvokeAsync
来检查请求是否来自回调路径(IIS),然后调用AuthenticateCoreAsync
。
protected async override System.Threading.Tasks.Task<AuthenticationTicket>
AuthenticateCoreAsync()
{
AuthenticationProperties properties = UnpackStateParameter(Request.Query);
if (properties != null)
{
var logonUserIdentity = Options.Provider.GetLogonUserIdentity(Context);
if (logonUserIdentity.AuthenticationType != Options.CookieOptions.AuthenticationType
&& logonUserIdentity.IsAuthenticated)
{
AddCookieBackIfExists();
ClaimsIdentity claimsIdentity = new ClaimsIdentity(
logonUserIdentity.Claims, Options.SignInAsAuthenticationType);
claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier,
logonUserIdentity.User.Value, null, Options.AuthenticationType));
var ticket = new AuthenticationTicket(claimsIdentity, properties);
var context = new MixedAuthAuthenticatedContext(
Context,
claimsIdentity,
properties,
Options.AccessTokenFormat.Protect(ticket));
await Options.Provider.Authenticated(context);
return ticket;
}
}
return new AuthenticationTicket(null, properties);
}
AuthenticateCoreAsync
使用 AddCookieBackIfExists
来读取由 AD 创建的声明 cookie,并创建自己的基于声明的 Claims。
AD 用户会得到与 Web 用户相同的基于声明的 Cookie。现在,AD 就像其他第三方认证者(Google、FB、LinkedIN)一样。
Install-Package OWIN-MixedAuth
https://github.com/MohammadYounes/Owin-MixedAuth - Dave Alperovich