asp.net-mvc 如何在 MVC 应用程序中切换身份验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20150410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to switch Authentication in MVC Application?
提问by Bryuk
I've created Internet MVC Application with Individual User AccountsAuthentication, but now this project should be intranet with windows authentication... How to switch authentication, when project is almost done? I'm not guru in MVC and this is new technology for me, so any help please and if possible with all steps in description=)
我已经创建了带有Individual User Accounts身份验证的Internet MVC 应用程序,但是现在这个项目应该是带有 Windows 身份验证的内网...当项目快完成时如何切换身份验证?我不是 MVC 的大师,这对我来说是新技术,所以请提供任何帮助,如果可能的话,请在描述中的所有步骤中提供帮助 =)
回答by Firearm
In the Web.config of you project. The first step would be change:
在您项目的 Web.config 中。第一步是改变:
<authentication mode="Forms">
</authentication>
to
到
<authentication mode="Windows">
</authentication>
Selecting your project and hitting F4 for the properties window allows you to change the authentication method.
选择您的项目并在属性窗口中按 F4 允许您更改身份验证方法。
However instead of me putting step by step in here just use this very easy to follow tutorial: Enabling Windows Authentication
然而,不是我一步一步在这里,而是使用这个非常容易遵循的教程: 启用 Windows 身份验证
回答by Adam R. Grey
Since I found this question through google attempting the same thing, and Firearm's link doesn't quite do the process justice, I'll attempt to list the steps I went through here. Obviously, if I tell you to remove something, that only means if you aren't using it otherwise. I don't think you have to do these steps in any particular order. Also, I'm using Entity Framework, so you'll have to look elsewhere to remove it.
由于我通过 google 尝试同样的事情发现了这个问题,而 Firearm 的链接并没有完全公平地处理这个过程,我将尝试列出我在这里经历的步骤。显然,如果我告诉您删除某些内容,那仅意味着您不以其他方式使用它。我认为您不必按任何特定顺序执行这些步骤。此外,我正在使用实体框架,因此您必须寻找其他地方才能将其删除。
- in the solution explorer, highlight your project and press f4. This will bring up the properties window for that project. Disable anonymous authentication. Enable windows authentication.
- Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution... uninstall anything with "owin" in the name, Microsoft.AspNet.Identity.EntityFramework, and Microsoft.AspNet.Identity.Core.
- Open your Web.config. Under runtime, under assemblyBinding, remove all the dependentAssembly's for Owin stuff that got left behind. Under system.web, replace
<authentication mode="None" />with<authentication mode="Windows" /> <authorization> <deny users="?" /> </authorization>. Under system.webServer, removehandlers. under modules, remove<remove name="FormsAuthentication" />. - Remove the Account and Manage controllers and views. Remove the
ManageViewModelsfrom your models. - Under App_Start, get rid of
IdentityConfigandStartup.Auth. - At the top level, right next to your web config, is Startup.cs. Get rid of it.
- Make a new ApplicationDbContext. It should derive from DbContext. Get rid of
throwIfV1Schema: falsein your constructors. Then you can get rid ofIdentityModelsfrom your Models folder. Add a new migration and update your database. - Obviously you'll have to clean out any references you've made yourself to Identity.
- 在解决方案资源管理器中,突出显示您的项目并按 f4。这将打开该项目的属性窗口。禁用匿名身份验证。启用 Windows 身份验证。
- 工具 -> NuGet 包管理器 -> 管理解决方案的 NuGet 包...卸载名称中带有“owin”、Microsoft.AspNet.Identity.EntityFramework 和 Microsoft.AspNet.Identity.Core 的任何内容。
- 打开您的 Web.config。在运行时下,在 assemblyBinding 下,删除所有遗留的 Owin 内容的相关程序集。在 system.web 下,替换
<authentication mode="None" />为<authentication mode="Windows" /> <authorization> <deny users="?" /> </authorization>. 在 system.webServer 下,删除handlers. 在模块下,删除<remove name="FormsAuthentication" />. - 删除帐户和管理控制器和视图。
ManageViewModels从您的模型中删除。 - 在 App_Start 下,去掉
IdentityConfig和Startup.Auth。 - 在顶层,就在您的 Web 配置旁边,是 Startup.cs。摆脱它。
- 创建一个新的 ApplicationDbContext。它应该派生自 DbContext。
throwIfV1Schema: false在你的构造函数中摆脱。然后你可以IdentityModels从你的模型文件夹中删除。添加新的迁移并更新您的数据库。 - 显然,您必须清除自己对 Identity 的任何引用。
Possible additional step: * remove _LoginPartial view. The _Layout view will then be updated to replace partial display of that view with this line:
可能的附加步骤: * 删除 _LoginPartial 视图。然后将更新 _Layout 视图以使用以下行替换该视图的部分显示:
<p class="nav navbar-text navbar-right">Hello, @User.Identity.Name!</p>
回答by chriszo111
Searching the exact same problem led me to this article, however the answers are a bit old, so with ASP.NET using MVC 5 this should be detailed documentation from Microsoft:
搜索完全相同的问题使我找到了这篇文章,但是答案有点旧,因此对于使用 MVC 5 的 ASP.NET,这应该是来自 Microsoft 的详细文档:
To detect Windows Authentication in an MVC project, the wizard looks for the
authenticationelement from your web.configfile.<configuration> <system.web> <authentication mode="Windows" /> </system.web> </configuration>To detect Windows Authentication in a Web API project, the wizard looks for the
IISExpressWindowsAuthenticationelement from your project's .csprojfile:<Project> <PropertyGroup> <IISExpressWindowsAuthentication>enabled </IISExpressWindowsAuthentication> </PropertyGroup> </Project>
为了检测 MVC 项目中的 Windows 身份验证,向导会
authentication从您的web.config文件中查找该元素。<configuration> <system.web> <authentication mode="Windows" /> </system.web> </configuration>为了检测 Web API 项目中的 Windows 身份验证,向导会
IISExpressWindowsAuthentication从项目的.csproj文件中查找元素:<Project> <PropertyGroup> <IISExpressWindowsAuthentication>enabled </IISExpressWindowsAuthentication> </PropertyGroup> </Project>
Found at Diagnosing errors with the Azure Active Directory Connection Wizard
在使用 Azure Active Directory 连接向导诊断错误中找到
For my specific problem it was switching to Azure AD rather than Windows Authentication (which was preset), there are more steps found at the developer network website.
对于我的具体问题,它切换到 Azure AD 而不是 Windows 身份验证(这是预设的),在开发者网络网站上可以找到更多步骤。
回答by Dennis Betten
I'm afraid I'm a bit late with my answer to you're question on how to implement the SwitchUser functionality, but for those of you who are still struggling with this (even Microsoft SharePoint still can't get it to work...), here's how it's done: (I just finished writing the article)
恐怕我对您关于如何实现 SwitchUser 功能的问题的回答有点晚了,但是对于那些仍在为此苦苦挣扎的人(即使 Microsoft SharePoint 仍然无法使其正常工作。 ..),这是怎么做的:(我刚写完这篇文章)
Switch User Functionality using MVC4 and Windows Authentication
If you need more information on how to get Windows Authentication workong for an Intranet Website using AD and Windows Server 2012 (or Higher), then take a look at my following article:
如果您需要有关如何使用 AD 和 Windows Server 2012(或更高版本)为 Intranet 网站获取 Windows 身份验证工作的更多信息,请查看我的以下文章:
Windows Authentication on Intranet Website using AD and Windows Server 2012 (or Higher)
使用 AD 和 Windows Server 2012(或更高版本)的 Intranet 网站上的 Windows 身份验证
Happy coding!
快乐编码!

