日期:2014-05-16 浏览次数:20405 次
Quote: 引用: Quote: 引用: Quote: 引用: Authorize是可以自定义的 在Model目录下新建一个MyAuthAttribute类,如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace AuthTest.Models { public class MyAuthAttribute : AuthorizeAttribute { // 只需重载此方法,模拟自定义的角色授权机制 protected override bool AuthorizeCore(HttpContextBase httpContext) { string currentRole = GetRole(httpContext.User.Identity.Name); if(Roles.Contains(currentRole ) ) return true; return base.AuthorizeCore(httpContext); } // 返回用户对应的角色, 在实际中, 可以从SQL数据库中读取用户的角色信息 private string GetRole(string name) { switch(name) { case "aaa": return "User"; case "bbb": return "Admin"; case "ccc": return "God"; default: return "Fool"; } } } } 修改HomeController, 如下using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Securit
Quote: 引用: Quote: 引用: Authorize是可以自定义的 在Model目录下新建一个MyAuthAttribute类,如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace AuthTest.Models { public class MyAuthAttribute : AuthorizeAttribute { // 只需重载此方法,模拟自定义的角色授权机制 protected override bool AuthorizeCore(HttpContextBase httpContext) { string currentRole = GetRole(httpContext.User.Identity.Name); if(Roles.Contains(currentRole ) ) return true; return base.AuthorizeCore(httpContext); } // 返回用户对应的角色, 在实际中, 可以从SQL数据库中读取用户的角色信息 private string GetRole(string name) { switch(name) { case "aaa": return "User"; case "bbb": return "Admin"; case "ccc": return "God"; default: return "Fool"; } } } } 修改HomeController, 如下using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Securit
Quote: 引用: Authorize是可以自定义的 在Model目录下新建一个MyAuthAttribute类,如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace AuthTest.Models { public class MyAuthAttribute : AuthorizeAttribute { // 只需重载此方法,模拟自定义的角色授权机制 protected override bool AuthorizeCore(HttpContextBase httpContext) { string currentRole = GetRole(httpContext.User.Identity.Name); if(Roles.Contains(currentRole ) ) return true; return base.AuthorizeCore(httpContext); } // 返回用户对应的角色, 在实际中, 可以从SQL数据库中读取用户的角色信息 private string GetRole(string name) { switch(name) { case "aaa": return "User"; case "bbb": return "Admin"; case "ccc": return "God"; default: return "Fool"; } } } } 修改HomeController, 如下using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Securit
Authorize是可以自定义的 在Model目录下新建一个MyAuthAttribute类,如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace AuthTest.Models { public class MyAuthAttribute : AuthorizeAttribute { // 只需重载此方法,模拟自定义的角色授权机制 protected override bool AuthorizeCore(HttpContextBase httpContext) { string currentRole = GetRole(httpContext.User.Identity.Name); if(Roles.Contains(currentRole ) ) return true; return base.AuthorizeCore(httpContext); } // 返回用户对应的角色, 在实际中, 可以从SQL数据库中读取用户的角色信息 private string GetRole(string name) { switch(name) { case "aaa": return "User"; case "bbb": return "Admin"; case "ccc": return "God"; default: return "Fool"; } } } } 修改HomeController, 如下using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Securit
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace AuthTest.Models { public class MyAuthAttribute : AuthorizeAttribute { // 只需重载此方法,模拟自定义的角色授权机制 protected override bool AuthorizeCore(HttpContextBase httpContext) { string currentRole = GetRole(httpContext.User.Identity.Name); if(Roles.Contains(currentRole ) ) return true; return base.AuthorizeCore(httpContext); } // 返回用户对应的角色, 在实际中, 可以从SQL数据库中读取用户的角色信息 private string GetRole(string name) { switch(name) { case "aaa": return "User"; case "bbb": return "Admin"; case "ccc": return "God"; default: return "Fool"; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Securit