asp.net-mvc-3 - 在 ASP.NET MVC3 应用程序中是否可以同时拥有多个身份验证提供程序?

标签 asp.net-mvc-3 httpwebrequest asp.net-membership authorization

客户端应用程序正在将音频文件以“ block ”形式上传到 MVC3 站点。客户端使用 HttpWebRequest POST 来完成此操作。

在服务器上,我有以下 Controller 操作:

  [Authorize]
  [HttpPost]
  public JsonResult RecieveChunk(string id, [ModelBinder(typeof(AudioChunkModelBinder))] byte[] audio)
        {
            //Process chunk

            var chunk = new AudioChunk
            {
                ThoughtId = Guid.Parse(id),
                Data = audio
            };

            //Process chunk by BL

            return new JsonResult {Data = "Success"};
        }

目前,内置的 AspNetMemebershipProvider 正在处理授权,因此客户端应用程序必须首先在登录页面进行身份验证,将 cookie 获取到 CookieContainer 中,然后进行调用服务器来上传一 block 数据。

我希望允许客户端能够匿名上传音频文件到服务器,而无需事先注册。每次从同一设备上传文件时,客户端应用程序代码都会提供相同的 guid。

我希望这两类用户共享相同的 RecieveChunk 操作来执行此操作。但它们必须以匿名方式(仅使用 guid)或使用登录/通行证组合进行授权。

我可以将两个不同的 Controller 链接到两个不同的身份验证提供程序吗?第三个 Controller 具有 [Authorize] 标记的操作,如果任一提供者已向用户提供 cookie(或某种其他身份验证方法),则将允许执行操作。

在 ASP.NET MVC3 中一般可能吗?

最佳答案

正如评论中所讨论的,您可以创建 FilterAttribute class 的自定义实现并实现 IAuthorizationFilter interface 。例如,这里是 ChildActionOnlyAttribute 实现:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class ChildActionOnlyAttribute : FilterAttribute, IAuthorizationFilter
{
  public void OnAuthorization(AuthorizationContext filterContext)
  {
    if (filterContext == null)
      throw new ArgumentNullException("filterContext");
    if (!filterContext.IsChildAction)
      throw Error.ChildActionOnlyAttribute_MustBeInChildRequest(filterContext.ActionDescriptor);
  }
}

这是 RequireHttpsAttribute 实现:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class RequireHttpsAttribute : FilterAttribute, IAuthorizationFilter
{
  public virtual void OnAuthorization(AuthorizationContext filterContext)
  {
    if (filterContext == null)
      throw new ArgumentNullException("filterContext");
    if (filterContext.HttpContext.Request.IsSecureConnection)
      return;
    this.HandleNonHttpsRequest(filterContext);
  }

  protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext)
  {
    if (!string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
      throw new InvalidOperationException(MvcResources.RequireHttpsAttribute_MustUseSsl);
    string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;
    filterContext.Result = (ActionResult) new RedirectResult(url);
  }
}

所以你可以这样做:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class CustomAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
  public void OnAuthorization(AuthorizationContext filterContext)
  {
    if (filterContext == null)
      throw new ArgumentNullException("filterContext");

    var guidPresent = CheckForGuid();

    if (!filterContext.HttpContext.User.Identity.IsAuthenticated && !guidPresent)
      throw new InvalidOperationException("Must authenticate properly")
  }
}

关于asp.net-mvc-3 - 在 ASP.NET MVC3 应用程序中是否可以同时拥有多个身份验证提供程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8919347/

相关文章:

c# - Entity Framework 5 更新记录

c# - 使用 https 和基本身份验证的 http web 请求

c# - 使用 C# 发送 POST 数据

c# - 部分 View 不会在 ajax.beginform 之后更新

c# - ASP.NET MVC : Ignore field(s) when editing

java - 在 Eclipse 中查看 http POST 和 GET 数据的最简单/最佳方法?任何插件/工具?

c# - 如何在 Asp.net 中扩展成员资格?

mysql - 查找用户首次登录asp.net成员(member)资格

asp.net - 更改数据库中的 ASP.NET 成员资格表

javascript - 从剪贴板粘贴所有浏览器的按钮