日期:2014-05-16  浏览次数:20405 次

asp.net mvc如何实现会员,店铺,管理员多种登录以及正确跳转?求指教
刚用身份验证还不熟悉,

我有会员,店铺,网站管理员三种用户,都有各自的登录页、管理中心
我需要的功能是会员进会员中心如果没有登录跳转到会员登录页,

店铺进入店铺中心如果没有登录跳转到店铺登录页,

网站管理员进入后台管理如果没有登录跳转到管理员登录页,


这种该如何用asp.net 身份验证实现呢?web.config该怎么配置呢?
求详细方法步骤
------解决方案--------------------
引用:
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