asp.net-mvc – ASP.NET MVC 4中的Windows身份验证和表单身份验证的混合
|
我们有一个ASP.NET MVC 4内部网应用程序.我们正在使用Windows身份验证,这方面工作正常.使用用户的凭据,我们可以从Web应用程序访问这些凭据. 然而,我们真正想要的是某种混合模式.我们希望从浏览器获取用户的凭据,但我们还想验证用户是否在我们的应用程序的数据库中.如果用户在数据库中,那么他们可以继续.如果不是,我们希望将它们重定向到要求备用凭据的页面.我现在正在做的是,在Global.asax.cs中,我有一个Application_AuthenticateRequest方法,我正在检查用户是否经过身份验证.如果它们和他们的cookie信息没有反映他们登录到系统的事实,那么我将他们登录并设置一些cookie,其中包含有关用户的信息.如果他们未经过身份验证,我会将其重定向到登录页面.我们不能将AD角色用于公司策略所涉及的原因,因此我们需要使用该数据库进行其他身份验证. 我猜测Application_AuthenticateRequest不是这样做的地方,但也许就是这样.但我们基本上需要一个地方来过滤身份验证请求.但另外这个实现引出了另一个问题: 我们的应用中有一些允许匿名访问的网址.我添加了< location>标记到web.config这些.问题是,当对这些进行匿名调用时,它会进入Application_AuthenticateRequest并尝试将用户登录到数据库中.现在,我可以在Application_AuthenticateRequest中添加代码来处理这些URL,这是我目前的计划,但是如果我正在编写并且Application_AuthenticateRequest不是这样做的地方,那么我现在要比以后更好地解决它. 解决方法您需要使用动作过滤器来实现此目的.您可以像这样扩展AuthorizeAttribute:public class MyAuthorizeAttribute : AuthorizeAttribute
{
private UnitOfWork _unitOfWork = new UnitOfWork();
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = false;
var username = httpContext.User.Identity.Name;
// Some code to find the user in the database...
var user = _unitOfWork.UserRepository.Find(username);
if(user != null)
{
isAuthorized = true;
}
return isAuthorized;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (AuthorizeCore(filterContext.HttpContext))
{
SetCachePolicy(filterContext);
}
else
{
// If not authorized,redirect to the Login action
// of the Account controller...
filterContext.Result = new RedirectToRouteResult(
new System.Web.Routing.RouteValueDictionary {
{"controller","Account"},{"action","Login"}
}
);
}
}
protected void SetCachePolicy(AuthorizationContext filterContext)
{
// ** IMPORTANT **
// Since we're performing authorization at the action level,// the authorization code runs after the output caching module.
// In the worst case this could allow an authorized user
// to cause the page to be cached,then an unauthorized user would later
// be served the cached page. We work around this by telling proxies not to
// cache the sensitive page,then we hook our custom authorization code into
// the caching mechanism so that we have the final say on whether a page
// should be served from the cache.
HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
cachePolicy.SetProxyMaxAge(new TimeSpan(0));
cachePolicy.AddValidationCallback(CacheValidationHandler,null /* data */);
}
public void CacheValidationHandler(HttpContext context,object data,ref HttpValidationStatus validationStatus)
{
validationStatus = OnCacheAuthorization(new HttpContextWrapper(context));
}
}
然后,您可以在Controller级别或Action级别使用此属性,如下所示: [MyAuthorize]
public ActionResult SomeAction()
{
// Code that is supposed to be accessed by authorized users only
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 为什么MVC而不是好的旧ASP.NET?还没有抓住为什么
- 单元测试传统ASP.NET Webforms应用程序
- asp.net – SQL Server Reporting Services – 运
- asp.net – 如何循环通过WebForms中的数据,就像在
- 有人创建安装程序来部署内部asp.net Web应用程序
- asp.net – 在IIS7中设置自定义错误页面时是否可
- 为什么(以及如何)ASP.NET缓存存储在非托管内存中
- asp.net-mvc – 从桌面开发人员的角度来学习ASP.
- asp.net-mvc – 十进制的最佳数据注释(18,2)
- asp.net按钮点击w / javascript“你确定吗?”在
