简体   繁体   English

如何将ASP.NET MVC Web应用程序的标识/身份验证部分移到类库中?

[英]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). 我移出了所有样板UserManagerSigninManager内容,并从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.csStartup.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.

相关问题 如何在没有身份的asp.net MVC中实现自定义身份验证/授权? - How can I implement custom authentication/authorization in asp.net MVC without Identity? ASP.NET MVC中的身份cookie身份验证 - Identity cookie authentication in ASP.NET MVC 如何为 ASP.NET MVC Web 应用程序中使用的图像添加缓存? - How do I add caching for images used in my ASP.NET MVC web app? 如何在MVC 3 Web应用程序中使用ASP.NET Session对象? - How do I use ASP.NET Session object in my MVC 3 web app? 如何将ASP.NET MVC 5 Web App部署到Azure中的虚拟网络 - How can I deploy an ASP.NET MVC 5 Web App to a Virtual Network in Azure 我可以将与ASP.NET身份验证相关的配置移出web.config吗 - Can I move asp.net authentication related configuration out of web.config 我如何在我的 asp.net MVC 核心 web 应用程序中引用 static 文件 - How i can reference static files inside my asp.net MVC core web application 类库中的ASP.NET身份参考 - ASP.NET Identity reference in a Class Library 我如何为asp.net Web应用程序构建类体系结构 - How can i build my class architecture for my asp.net web application 类库参考在Asp.net 5 MVC 6 Web应用程序中不起作用 - Class library reference is not working in Asp.net 5 mvc 6 web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM