代理-客户关系



我正在尝试开发一个应用程序,在该应用程序中,一个用户(称为Agent)在向网站注册后可以创建多个客户端。我正在使用MVC5和ASP.NET Identity。我想知道的是,当代理处理特定客户端时,我是否需要将客户端id存储在会话中,或者每次我以查询字符串或类似的方式发送id时,以识别代理正在处理的当前客户端。如果有人能指导我朝着正确的方向前进,我将不胜感激。

示例类如下

public class Agent
{
  public int UserId{get; set;}
  public ICollection<Client> Clients{get; set;}
  public virtual ApplicationUser User{get; set;}
}
public class Client
{
  public int ClientId{get; set;}
  public string Name{get; set}
  public Address Address{get; set;}
  public Something1 Something1{get; set;}
  public Something2 Something2{get; set;}
  so on....
  public int UserId{get; set;}
  public virtual Agent Agent{get; set;}
}
public class Address
{
  public int ClientId{get; set;}
  public string Line1{get; set}
  public string Line2{get; set}
  public string PostCode{get; set}

  public virtual Client Client{get; set;}
}

根据REST准则,客户端是一个资源,应该由URL标识。例如,/clients/{clientId}用于详细信息,/clients/{clientId}/edit用于修改,/clients/{clientId}/delete用于删除等。

您的操作将clientId作为参数,然后使用该参数查找相应的客户端。对于用户级权限,只需将拥有客户端对象的用户集成到查询中即可。例如:

var userId = User.Identity.GetUserId();
var client = db.Clients.SingleOrDefault(m => m.UserId == userId && m.ClientId == clientId);
if (client == null)
{
    return new HttpNotFoundResult();
}
// whatever

最新更新