Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多个授权请求_C#_Asp.net Mvc_Asp.net Mvc 3_Razor - Fatal编程技术网

C# 多个授权请求

C# 多个授权请求,c#,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,我是MVC新手,甚至还没有真正掌握[Authorize]的窍门。基本上,我有一个网站,其中一个领域要求他们在访问之前提供自己的电子邮件地址、姓名和公司。没有密码,没有注册等。然后将有一个管理员区域,要求用户使用其电子邮件地址和密码登录 我的问题是,我将如何实施双重授权 还忘了提到,当在管理区域时,他们可以将材料上传到他们管理的站点,即他们当前所在的站点以外的站点。我有一个数据库,保存哪些网站是他们的管理员。URL类似于/Admin/Site/但一旦他们登录到Admin,我如何确保他们不能进入/A

我是MVC新手,甚至还没有真正掌握
[Authorize]
的窍门。基本上,我有一个网站,其中一个领域要求他们在访问之前提供自己的电子邮件地址、姓名和公司。没有密码,没有注册等。然后将有一个
管理员
区域,要求用户使用其电子邮件地址和密码登录

我的问题是,我将如何实施双重授权


还忘了提到,当在
管理
区域时,他们可以将材料上传到他们管理的站点,即他们当前所在的站点以外的站点。我有一个数据库,保存哪些网站是他们的管理员。URL类似于/Admin/Site/但一旦他们登录到Admin,我如何确保他们不能进入/Admin/Site2,因为
Site2
不是他们的管理员。

我假设此人可以匿名下载文件,只要她/他之前提供了详细信息。在这种情况下,忽略Authorization属性并编写自己的。下面是一个简单的例子。它依赖于要设置的cookie

public class CheckEmailAddressAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var request = filterContext.HttpContext.Request;
        // if the cookie isn't set the a user never provided their details
        if (request.Cookies["mycookiename"] == null)
        {
            // Set it to the correct result, to redirect the user to provide their email address
            filterContext.Result = new ViewResult { ViewName = "GetEmailAddress" };
            filterContext.HttpContext.Response.AppendCookie(new HttpCookie("mycookiename", "true"));
        }
        else
        {
            // Don't do anything, the user already provided their email address
        }

    }
}
并在进行下载的控制器上这样指定它

public class DownloadController : Controller
{
    [CheckEmailAddress]
    public ActionResult Download(string name)
    {
        // Implement download
    }    
}
唯一剩下的就是在设置电子邮件地址时设置cookie。我想你知道怎么做。您可能还希望确保您有一些“returnUrl”参数,以便在用户提供其电子邮件地址后,您可以将用户重定向到下载页面

编辑

根据OP,我们不想设置cookie,除非此人输入其详细信息,所以这里是更新的类

public class CheckEmailAddressAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var request = filterContext.HttpContext.Request;
        // if the cookie isn't set the a user never provided their details
        if (request.Cookies["mycookiename"] == null)
        {
            // Set it to the correct result, to redirect the user to provide their email address
            filterContext.Result = new ViewResult { ViewName = "GetEmailAddress" };
            // filterContext.HttpContext.Response.AppendCookie(new HttpCookie("mycookiename", "true"));
        }
        else
        {
            // Don't do anything, the user already provided their email address
        }

    }
}

您需要使用角色。[[1]:例如,我会有[CheckEmailAddress]在新闻区域内的所有内容上,除了询问其详细信息的页面之外?同样,在本例中,他们所要做的就是进入页面并设置cookie。他们可以重定向到页面,然后简单地向后导航,然后他们就有了cookie。请参阅我的更新。cookie不应该在属性中设置。那么,coo在哪里kie现在设置了吗?这取决于你在哪里添加它。你可以将它添加到全局过滤器、控制器或操作中。我没有太多使用区域,因此不知道你是否可以为“区域”中的所有控制器注册操作过滤器。