[英]How can I move the identity/authentication part of my ASP.NET MVC web app into a class library?
I have an ASP.NET MVC 5 web application, using ASP.NET Identity 2.1. 我有一个使用ASP.NET Identity 2.1的ASP.NET MVC 5 Web应用程序。 I'm trying to move all the identification / authorization code out of the web project into a class library.
我正在尝试将所有标识/授权代码从Web项目中移出到类库中。
I moved all the boilerplate UserManager
, SigninManager
stuff out, and removed all the NuGet packages for ASP.NET Identity and OWIN from the web project (adding those to the class library). 我移出了所有样板
UserManager
和SigninManager
内容,并从Web项目中删除了ASP.NET Identity和OWIN的所有NuGet程序包(将它们添加到类库中)。
I also moved the code from the Startup.cs
and Startup.Auth.cs
files into my class library. 我还将代码从
Startup.cs
和Startup.Auth.cs
文件移到了我的类库中。 These contain the OWIN configuration stuff. 这些包含OWIN配置资料。
How do I now ensure that this code is invoked when the application starts up? 现在如何确保在应用程序启动时调用此代码? In the original project, the
Startup
class (in Startup.cs
) had a line like this: 在原始项目中,
Startup
类(在Startup.cs
)有这样一行:
[assembly: OwinStartupAttribute(typeof(WebApplication1.Startup))]
I've put a similar line (with the correct class name) in the equivalent file in my class library, but that seems to have no effect. 我在类库的等效文件中放置了类似的行(具有正确的类名),但这似乎没有效果。 Does this line have to go in the web app?
此行是否必须在网络应用程序中显示? (I which case, presumably I'd need to add the OWIN NuGet package back into the web project - something I'd hoped to avoid).
(在那种情况下,大概我需要将OWIN NuGet包重新添加到Web项目中-我希望避免这种情况)。
I also tried adding this to my web.config file: 我还尝试将其添加到我的web.config文件中:
<add key="owin:appStartup" value="MyClassLibrary.MyNamespace.Startup, My.DLL.Name" />
But again, I'm not sure exactly how that would work without something within the web project triggering it - which again comes back to adding OWIN to the web project. 但是同样,我不确定在没有Web项目中的任何东西触发的情况下该如何工作-再次回到将OWIN添加到Web项目中。
I'm so confused - any help would be appreciated. 我很困惑-任何帮助将不胜感激。
From the http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx/ 从http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx/
PreApplicationStartMethodAttribute PreApplicationStartMethodAttribute
This new attribute allows you to have code run way early in the ASP.NET pipeline as an application starts up. 此新属性使您可以在应用程序启动时在ASP.NET管道中尽早运行代码。 I mean way early, even before Application_Start.
我的意思是早一点,甚至在Application_Start之前。
PreApplicationStartMethodAttribute is where Microsoft.Owin.Host.systemWeb gets initialised. PreApplicationStartMethodAttribute是初始化Microsoft.Owin.Host.systemWeb的位置。 Hence you need to add reference to Microsoft.Owin.Host.systemWeb in your web app.
因此,您需要在Web应用程序中添加对Microsoft.Owin.Host.systemWeb的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.