1.定义身份实体对象

     /// <summary>
/// 网站用户实体对象
/// </summary>
public class DDTPrincipal : IPrincipal
{
public int? OrgCode { get; set; }
public string RoleName { get; set; }
public string OrgName { get; set; }
private IIdentity _identity; public DDTPrincipal(string orgcode, string roleName, string orgName, IIdentity identity)
{
int _orgCode;
int.TryParse(orgcode, out _orgCode);
OrgCode = _orgCode;
OrgName = orgName;
RoleName = roleName;
_identity = identity;
} public IIdentity Identity
{
get { return _identity; }
} public bool IsInRole(string role)
{
return RoleName.IndexOf(role)>=;
}
}

2.验证身份

         [HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult LoginView(LoginModel model, string returnUrl)
{ if (ModelState.IsValid)
{
Account a = DataRepository.AccountProvider.GetByAccountName(model.UserName);
DataRepository.AccountProvider.DeepLoad(a,false, DeepLoadType.IncludeChildren,typeof(Org));
TList<AccountRole> arList = DataRepository.AccountRoleProvider.GetByAccountName(a.AccountName);
DataRepository.AccountRoleProvider.DeepLoad(arList, false, DeepLoadType.IncludeChildren, typeof(Role)); string roleName=string.Empty;
if (arList.Count > )
{
foreach (var item in arList)
{
roleName += item.RoleNoSource.RoleName + ",";
}
}
else { roleName = "无"; } if (a!=null&&a.AccountPassword==model.Password)
{
// return RedirectToLocal(returnUrl);
FormsAuthentication.RedirectFromLoginPage(a.UserName, false);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(, a.UserName, DateTime.Now,
DateTime.Now.AddMinutes(), false,
string.Format("{0}|{1}|{2}", a.OrgCode.Value.ToString(),roleName,a.OrgCodeSource.OrgName));
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(cookie);
return Redirect(returnUrl);
}
}
// 如果我们进行到这一步时某个地方出错,则重新显示表单
ModelState.AddModelError("", "提供的用户名或密码不正确。");
return View(model); //if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
//{
// return RedirectToLocal(returnUrl);
//}
//// 如果我们进行到这一步时某个地方出错,则重新显示表单
//ModelState.AddModelError("", "提供的用户名或密码不正确。");
//return View(model);
}

3.自定义验证属性获取验证信息

     public class CustomAuthorizeAttribute:AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
httpContext.User = App_Codes.WebUtility.GetUser(httpContext);
return base.AuthorizeCore(httpContext);
}
}

4.从验证信息生成验证对象

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Security.Principal; namespace XXX.App_Codes
{
public static class WebUtility
{
public static DDTPrincipal GetUser(HttpContextBase httpContext)
{
if (httpContext.Request.IsAuthenticated)
{
FormsIdentity fi = httpContext.User.Identity as FormsIdentity;
if (fi != null)
{
string[] userData = fi.Ticket.UserData.Split('|');
if (userData.Length == )
{
DDTPrincipal newPrincipal = new DDTPrincipal(userData[],
userData[],userData[],
httpContext.User.Identity);
return newPrincipal;
}
return null;
}
return null;
}
return null;
}
}
}

5.应用验证属性

     [CustomAuthorize]
public class CompanyManageController : Controller{}

6.配置窗体验证

<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

最新文章

  1. 3-EM的安装和使用
  2. Picture intermediate frame ----- increase smooth
  3. sqlServer 2008修改字段类型和重命名字段名称的sql语句
  4. NashZhou的自我介绍
  5. Python学习笔记——文件写入和读取
  6. 2. hdfs
  7. ORA-12516: TNS: 监听程序无法找到匹配协议栈的可用句柄解决方法
  8. jquery源码分析学习地址
  9. Java API —— System类
  10. C#中给线程传值并启动
  11. 转:微信生成二维码java
  12. 第一章:shiro简介
  13. 测试页面,页面里边一次加载50张不同的图片,每张5M以上,查看浏览器的内存使用情况
  14. 存在多个 AJAX 任务
  15. spring 读取yaml配置文件
  16. stark组件配置,二层URL
  17. Java元注解—— @Retention @Target @Document @Inherited
  18. Python中的filter()函数的用法
  19. SQL Server 2016 的JSON功能
  20. ExtJS 4.2 教程-05:客户端代理(proxy)

热门文章

  1. Cocos2d-x-3.0 Touch事件处理机制
  2. Objective C ARC 使用及原理
  3. mysql设置外网访问
  4. leetcode 191
  5. sqlserver如何关闭死锁进程.
  6. High Memory in the Linux Kernel
  7. vim命令的妙用
  8. Android之使用Android-query框架进行开发(一)(转载)
  9. Nginx ssl证书部署
  10. 监控平台项目之CSS总结——基于angularjs、bootstrap、jquery等框架