如何在 Web api 中启用 windows 身份验证和 Owin 令牌身份验证?
How to enable windows authentication in conjunction with Owin token authentication in web api?
我正在开发 Web api 应用程序,我需要按以下方式检查经过身份验证的用户:
1) 使用 Windows 身份验证
对用户进行身份验证
2) 如果未在 Windows 中进行身份验证。我将尝试使用 Owin 访问令牌.
对用户进行身份验证
我的代码可以正常工作,但是当我启用 Windows 身份验证时,如下所示:
public static IAppBuilder EnableWindowsAuthentication(this IAppBuilder app)
{
if (!app.Properties.TryGetValue("System.Net.HttpListener", out var val))
return app;
if (val is HttpListener listener)
{
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
}
return app;
}
然后里面 Startup.cs:
public void Configuration(IAppBuilder app)
{
OnAppDisposing(app);
ConfigureOAuth(app);
var config = new HttpConfiguration();
var webApiConfiguration = WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.EnableWindowsAuthentication();
//here some owin middlewares
app.UseWebApi(webApiConfiguration);
}
private void ConfigureOAuth(IAppBuilder app)
{
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
app.UseOAuthAuthorizationServer(OAuthServerOptions);
}
如果我尝试使用 Bearer 令牌调用授权端点,我会得到 401 UnAuthorized。
所以我的问题是:如何解决这种情况并使两种身份验证方法协同工作?
我是这样解决的:
在 SimpleAuthorizationServerProvider class 内部 GrantResourceOwnerCredentials 方法中,我将使用以下代码检查 Active Directory 中的用户:
public string FindUser(string activeDirectoryPath ,string userName, string password)
{
try
{
using (var searcher = new DirectorySearcher(new DirectoryEntry(activeDirectoryPath, userName, password)))
{
searcher.Filter = string.Format("(&(objectClass=user)(name={0}))", userName);
searcher.PropertiesToLoad.Add("name");// username
var activeDirectoryStaff = searcher.FindOne();
if (activeDirectoryStaff != null)
{
return (string)activeDirectoryStaff.Properties["name"][0];
}
else
return null;
}
}
}
catch (Exception ex)
{
this.Log().Error(ex, ex.Message);
return null;
}
return null;
}
如果上面的方法return为null,那么我会return 401 UnAuthorized .
我正在开发 Web api 应用程序,我需要按以下方式检查经过身份验证的用户:
1) 使用 Windows 身份验证
对用户进行身份验证2) 如果未在 Windows 中进行身份验证。我将尝试使用 Owin 访问令牌.
对用户进行身份验证我的代码可以正常工作,但是当我启用 Windows 身份验证时,如下所示:
public static IAppBuilder EnableWindowsAuthentication(this IAppBuilder app)
{
if (!app.Properties.TryGetValue("System.Net.HttpListener", out var val))
return app;
if (val is HttpListener listener)
{
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
}
return app;
}
然后里面 Startup.cs:
public void Configuration(IAppBuilder app)
{
OnAppDisposing(app);
ConfigureOAuth(app);
var config = new HttpConfiguration();
var webApiConfiguration = WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
app.EnableWindowsAuthentication();
//here some owin middlewares
app.UseWebApi(webApiConfiguration);
}
private void ConfigureOAuth(IAppBuilder app)
{
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
app.UseOAuthAuthorizationServer(OAuthServerOptions);
}
如果我尝试使用 Bearer 令牌调用授权端点,我会得到 401 UnAuthorized。
所以我的问题是:如何解决这种情况并使两种身份验证方法协同工作?
我是这样解决的:
在 SimpleAuthorizationServerProvider class 内部 GrantResourceOwnerCredentials 方法中,我将使用以下代码检查 Active Directory 中的用户:
public string FindUser(string activeDirectoryPath ,string userName, string password)
{
try
{
using (var searcher = new DirectorySearcher(new DirectoryEntry(activeDirectoryPath, userName, password)))
{
searcher.Filter = string.Format("(&(objectClass=user)(name={0}))", userName);
searcher.PropertiesToLoad.Add("name");// username
var activeDirectoryStaff = searcher.FindOne();
if (activeDirectoryStaff != null)
{
return (string)activeDirectoryStaff.Properties["name"][0];
}
else
return null;
}
}
}
catch (Exception ex)
{
this.Log().Error(ex, ex.Message);
return null;
}
return null;
}
如果上面的方法return为null,那么我会return 401 UnAuthorized .