简体   繁体   English

asp.net扩展IPrincipal

[英]asp.net extending IPrincipal

I would like to extend IPrincipal in asp.net to allow me to get the usertype that I will define. 我想在asp.net中扩展IPrincipal以允许我获得我将定义的usertype。 I would like to make it possible to do this in a controller 我想在控制器中做到这一点

string type = User.UserType 

then in my extension method i will have a method like 然后在我的扩展方法中,我将有一个方法,如

public string UserType()
{
  // do some database access 
  return userType

}

how can I do this? 我怎样才能做到这一点? is it possible? 可能吗? Thanks! 谢谢!

You can make an extension method: 您可以制作扩展方法:

public static string UserType(this IPrincipal principal) {
    // do some database access 
    return something;
}

Sure. 当然。 Make your class implements IPrincipal: 让你的类实现IPrincipal:

public class MyPrinciple : IPrincipal {
    // do whatever
}

Extension method: 扩展方法:

public static string UserType(this MyPrinciple principle) {
    // do something
}

Here is an example custom class that implements IPrincipal. 这是一个实现IPrincipal的示例自定义类。 This class includes a few extra methods to check for role affiliation but shows a property named UserType per your requirements. 此类包含一些额外的方法来检查角色关联,但根据您的要求显示名为UserType的属性。

  public class UserPrincipal : IPrincipal
  {
    private IIdentity _identity;
    private string[] _roles;

    private string _usertype = string.Empty;


    public UserPrincipal(IIdentity identity, string[] roles)
    {
      _identity = identity;
      _roles = new string[roles.Length];
      roles.CopyTo(_roles, 0);
      Array.Sort(_roles);
    }

    public IIdentity Identity
    {
      get
      {
        return _identity;
      }
    }

    public bool IsInRole(string role)
    {
      return Array.BinarySearch(_roles, role) >= 0 ? true : false;
    }

    public bool IsInAllRoles(params string[] roles)
    {
      foreach (string searchrole in roles)
      {
        if (Array.BinarySearch(_roles, searchrole) < 0)
        {
          return false;
        }
      }
      return true;
    }

    public bool IsInAnyRoles(params string[] roles)
    {
      foreach (string searchrole in roles)
      {
        if (Array.BinarySearch(_roles, searchrole) > 0)
        {
          return true;
        }
      }
      return false;
    }

    public string UserType
    {
      get
      {
        return _usertype;
      }
      set
      {
        _usertype = value;
      }
    }

  }

Enjoy! 请享用!

Basically, no. 基本上没有。 You can implement IPrincipal with a class such as MyPrincipal , and that class can have a UserType property, but you'd have to access the instance through a reference of its own type in order to reach it, not through the interface reference. 您可以使用MyPrincipal等类实现IPrincipal ,并且该类可以具有UserType属性,但您必须通过其自己类型的引用访问该实例才能到达它,而不是通过接口引用。

edit 编辑

An extension method could work, but only if you're absolutely certain you'll never call it on something that implements IPrincipal but isn't an instance of your own class. 扩展方法可以工作,但只有当你绝对肯定你永远不会在实现IPrincipal但不是你自己的类的实例的东西上调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM