我之前从未使用Windows身份验证用于ASP.NET MVC Web应用程序,但是表单身份验证.最近,我有一个ASP.NET MVC 4 Web应用程序,需要为被授权登录我公司Web服务器的用户实施Windows身份验证.所以,我对Windows身份验证有一些疑问.我正在使用Visual Studio 2012.
Windows身份验证如何工作?
如何在web.config文件中正确实现Windows身份验证?
如何测试Windows身份验证是否真的适用于我的ASP.NET MVC 4网站?换句话说,我如何在本地开发PC上使用本地IIS(版本8)和我公司的真实Web服务器上使用IIS版本7进行测试?
我知道我可以使用web.config中的授权标记来限制对ASP.NET MVC 3应用程序的访问
<authentication mode="Windows"></authentication>
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider" />
<authorization>
<allow roles="MyDomain\MyGroup" />
<deny users="*" />
<deny users="?" />
</authorization>
或使用[Authorize()]属性(甚至使用自定义Authorize属性)装饰控制器基类
[AdminOnly]
public class BaseController : Controller{}
Run Code Online (Sandbox Code Playgroud)
问题是:他们是替代方法和等效方法吗?我应该总是使用一种方法而不是另一种方法吗?我应该记住哪些元素?
我一直在网上阅读大量有关身份验证和授权的内容,最后确定了一些似乎正在运行的代码......但我并不完全理解它所做的一切(就FormsAuthenticationTicket而言).
我正在使用的MVC应用程序将处理一些敏感数据,我想要检查与验证和授权相关的所有操作.
我正在使用Windows身份验证;
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)
我在SQL Server中有一组表,其中包含其他用户和权限信息.
在我的Global.asax中,我有以下方法的启发
http://www.codeproject.com/Articles/5182/Insight-into-Security-Model-using-Principal-and-Id
protected void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs e)
{
if (e.Identity == null || !e.Identity.IsAuthenticated)
{
//Redirect to error or access denied page
}
string userData = e.Identity.AuthenticationType;
var cachedUser = HttpContext.Current.Cache[e.Identity.Name] as User;
if (cachedUser == null)
{
var user = repo.GetUserByFullUserName(e.Identity.Name);
HttpContext.Current.Cache.Insert(e.Identity.Name, user, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
cachedUser = HttpContext.Current.Cache[e.Identity.Name] as User;
}
var userIdentity = e.Identity.Name;
var formsAuthTicket = new FormsAuthenticationTicket(1, e.Identity.Name, …Run Code Online (Sandbox Code Playgroud) cookies forms-authentication windows-authentication asp.net-mvc-4
我是MVC的新手.我可以将MVC 5.2集成到我现有的Web表单Visual Studio 2012 Update 4项目中.我创建了我的第一个控制器,并且按预期工作.甚至在访问MVC视图时我也能够利用现有项目中的Windows窗体身份验证.但是当创建我的第二个控制器时,它开始变得混乱.
这是我的路线映射:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.EnableFriendlyUrls();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个控制器都位于〜/ Controllers中.我的第一个控制器是:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
//return View();
return Redirect("~/Default.aspx");
}
public ActionResult CloseSession()
{
return Redirect("http://www.yahoo.com");
}
}
Run Code Online (Sandbox Code Playgroud)
第二个控制器:
public class CajaWebController : Controller
{
//
// GET: …Run Code Online (Sandbox Code Playgroud) 我正在尝试保护我的暂存站点(mvc5应用程序),目前正在执行以下操作:
public class HomeController : Controller
{
public ActionResult Index()
{
if (Request.IsAuthenticated){
return View();
}
return RedirectToAction("Login", "Account");
}
Run Code Online (Sandbox Code Playgroud)
以上是太费力了,不能正确,我确信我不应该通过身份验证检查单独包装我的所有视图,但是我似乎无法在一个位置添加支票?
我有以下问题.在我的解决方案中,我有2个控制器:主页和帐户.到目前为止一切都有效.但是当我在HomeController上面添加[Authorize]时,例如
[Authorize]
public class HomeController : Controller
{
...
Run Code Online (Sandbox Code Playgroud)
我明白了
System.InvalidOperationException: The partial view 'Repository' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Account/Repository.aspx
~/Views/Account/Repository.ascx
~/Views/Shared/Repository.aspx
~/Views/Shared/Repository.ascx
~/Views/Account/Repository.cshtml
~/Views/Account/Repository.vbhtml
~/Views/Shared/Repository.cshtml
~/Views/Shared/Repository.vbhtml
Run Code Online (Sandbox Code Playgroud)
部分视图存储库位于HomeController内部,因此其View位于〜/ Views/Home/Repository.cshtml中,但它在Shared或Account文件夹中存在.正如我所说的,如果HomeController类之上没有[Authorize],那么一切都按预期工作.
发生错误的代码在_Layout.cshtml中
@Html.Partial("Repository")
Run Code Online (Sandbox Code Playgroud)
感谢帮助.