【发布时间】:2017-08-16 23:04:45
【问题描述】:
我继承了一个使用 OWIN 开发的项目,它是管理一种 WebApi 的服务器中间件,以便使用 ApiKeys 与移动设备进行通信。服务器端有一个小的 Web 界面(实际上是一组测试页面),但没有添加身份验证。我正试图围绕正在使用的不同框架以及围绕这些 OWIN 技术进行身份验证的方式展开思考。
让我先展示一下我拥有的东西:
public class Startup
{
public void Configuration(IAppBuilder app)
{
log.Info("RT Server app starting up ...");
// Initialize the ApiKey Needed for ApiClient Library
ApiClient.ApiKey = Globals.ApiKey;
// Initialize the Services Library
Services.Startup.Initialize();//creates a configuration map of values for devices
// Setup Server Middleware
app.Use(typeof(ServerMiddleware), "RTrak.Server", "RTrak.Server");
app.Use(typeof(ServerMiddleware), "RTrak.Server.Pages", "RTrak.Server");
// HttpListener listener = (HttpListener)app.Properties["System.Net.HttpListener"];//throws an KeyNotFoundException
// listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
//ConfigureAuth(app)
}
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = MyAuthentication.ApplicationCookie,
LoginPath = new PathString("/Login"),
Provider = new CookieAuthenticationProvider(),
CookieName = "RTrakCookie",
CookieHttpOnly = true,
ExpireTimeSpan = TimeSpan.FromHours(12), // ...
});
}
服务器中间件
public ServerMiddleware(OwinMiddleware next, string baseNamespace, string defaultClass) : base(next)
{
BaseNamespace = baseNamespace;
DefaultClass = defaultClass;
}
public override async Task Invoke(IOwinContext owinContext)
{
var absolutePath = owinContext.Request.Uri.AbsolutePath;
string serverNamespace = BaseNamespace;
Type type;
string classToLoad = "";
if (absolutePath == "/")
classToLoad = DefaultClass;
else
{
classToLoad = absolutePath.Substring(1).Replace('/', '.');
if (classToLoad.EndsWith("."))
classToLoad = classToLoad.Substring(0, classToLoad.Length - 1);
}
type = Type.GetType($"{serverNamespace}.{classToLoad}, {serverNamespace}", false, true);
if (type == null)
{
classToLoad += ".Default";
type = Type.GetType($"{serverNamespace}.{classToLoad}, {serverNamespace}", false, true);
}
if (type != null)
{
try
{
object objService = Activator.CreateInstance(type);
((Resource)objService).Execute(owinContext);
}
catch (System.MissingMethodException)
{
//"403 INVALID URL");
}
}
else
await Next.Invoke(owinContext);
}
}
ServerMiddleware 首先调用 Default Pages 类,它是链接到其他测试页面的 HTML 标记
一个想法是添加一个带有 AdAuthenticationService 管理 cookie 模型的 MVC LoginController 来管理配置为 ConfigAuth(app) 行中记录的启动的一部分的登录,但中间件忽略了控制器。 MVC 在这里合适吗?
然后,我正在查看此 ServerMiddleware 并尝试了解如何使用 ActiveDirectory 身份验证拦截默认页面浏览器调用。
我知道我可能忽略了一些东西。非常感谢您提供的任何东西(建议或资源)来帮助我解决这个困惑。
【问题讨论】:
标签: asp.net authentication owin