简体   繁体   English

授权标记在其他DLL中不起作用

[英]Authorize Tag not working in different DLL

I have two projects 我有两个项目

  • Web Api Web API
  • DLL Class Library DLL类库

The Web Api references classes and methods in the DLL. Web Api引用DLL中的类和方法。 The Web Api also uses out of the box ASP.NET Identity for security. Web Api还使用现成的ASP.NET Identity来确保安全。

The Web Api controller methods are secured with [Authorize] tags as shown below: Web Api控制器方法由[Authorize]标签保护,如下所示:

[Authorize]
[HttpGet]        
[Route("", Name = "GetStuff")]        
public IHttpActionResult Get()        
{            
List<Stuff> Stuffs= LookUpBusinessLogic.StuffGetAll();            
return Ok(Stuffs);        
}

The DLL also has these tags securing its method calls as such: DLL还具有以下标记,以确保其方法调用的安全性:

[Authorize]
public static List<Stuff> StuffGetAll()        
{
//Get data from somewhere
...            
return Stuffs;        
}

The problem I am facing is that the authorize tag is not working in the DLL method. 我面临的问题是DLL方法中的authorize标记不起作用。

It is possible that I am completely missing how the calling user's identity gets passed through. 我可能完全不知道呼叫用户的身份是如何传递的。 I'd like to ask: 我想问一下:

Does the calling users identity get passed between DLLs? 主叫用户身份是否在DLL之间传递?

If not is there a way to secure the DLL methods based on the user identity in my example? 如果没有,在我的示例中是否有一种方法可以基于用户身份保护DLL方法?

[Authorize] is an attribute which should be used only with action methods. [Authorize]是仅应与操作方法一起使用的属性。 It won't be triggered for your custom static method. 您的自定义静态方法不会触发该事件。

Look at its signature: 看一下它的签名:

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, Inherited = true, 
AllowMultiple = true)]
public class AuthorizeAttribute : FilterAttribute, IAuthorizationFilter

Since it inherits from FilterAttribute you know it can be triggered eg for OnActionExecuting and OnActionExecuted methods of filter. 因为它继承自FilterAttributeFilterAttribute您知道它可以被触发,例如针对Filter的OnActionExecutingOnActionExecuted方法。

Your code is compiling because this attribute allows usage in methods. 您的代码正在编译,因为此属性允许在方法中使用。 This doesn't mean it has any functionality when used outside controller context. 这并不意味着在控制器上下文之外使用时它具有任何功能。

To be honest I don't understand your need of allowing DLL access for specific users. 老实说,我不理解您是否需要允许特定用户的DLL访问。 If this is some kind of class library, you should check access only in classes which are using methods from external DLL. 如果这是某种类库,则应仅在使用外部DLL方法的类中检查访问。

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

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