简体   繁体   English

IAuthenticationFilter 的 .NET Core 等价物是什么?

[英]What is the .NET Core equivalent of IAuthenticationFilter?

How can authentication be done in .NET Core as it was done using IAuthenticationFilter?如何像使用 IAuthenticationFilter 那样在 .NET Core 中完成身份验证?

public class CustomAuthenticationFilter : ActionFilterAttribute, IAuthenticationFilter  
{  
    void IAuthenticationFilter.OnAuthentication(AuthenticationContext filterContext)  
    {  
        if (string.IsNullOrEmpty(Convert.ToString(filterContext.HttpContext.Session["Username"])))  
        {  
            filterContext.Result = new HttpUnauthorizedResult();  
        }  
    }  

    void IAuthenticationFilter.OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)  
    {  
        if (filterContext.Result == null || filterContext.Result is HttpUnauthorizedResult)  
        {  
            filterContext.Result = new RedirectToRouteResult(  
                new RouteValueDictionary {  
                { "controller", "Account" },  
                { "action", "Login" } });  
        }  
    }  
}  

I am a newbie with .NET Core and filters.我是 .NET 核心和过滤器的新手。 I was trying to authenticate users with filters before they can be allowed to access an action in a controller. Using this in .NET Core gives an error我试图在允许用户访问 controller 中的操作之前使用过滤器对用户进行身份验证。在 .NET Core 中使用它会出错

"Error CS7069 Reference to type 'HttpContextBase' claims it is defined in 'System.Web', but it could not be found". “错误 CS7069 对类型‘HttpContextBase’的引用声称它是在‘System.Web’中定义的,但找不到它”。

After searching this error I found that this is only available in .NET Framework.搜索此错误后,我发现这仅在 .NET Framework 中可用。 How do I do authentication with filters in .NET Core?如何在 .NET Core 中使用过滤器进行身份验证?

Asp.net core doesn't contain the IAuthenticationFilter, if you want to authenticated the user, I suggest you could try to refer to this article . Asp.net 核心不包含IAuthenticationFilter,如果你想对用户进行认证,我建议你可以参考这篇文章

If you want to check the user's claim like roles before go to the controller, you could consider using the IAuthorizationFilter.如果你想检查用户的声明,比如go之前的角色到controller,你可以考虑使用IAuthorizationFilter。

More details, you could refer to below codes:更多详细信息,您可以参考以下代码:

public class CustomAuthorizationFilterAttribute : Attribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationFilterContext context)
    {
        string currentUserRole = Convert.ToString(context.HttpContext.Session.GetString("UserRole"));

        if (!string.IsNullOrEmpty(currentUserRole))
        {
            if (currentUserRole != "Admin")
            {
                context.Result = new RedirectToRouteResult
            (
            new RouteValueDictionary(new
            {
                action = "Error",
                controller = "Error"
            }));

            }
            else
            {
                context.Result = new RedirectToRouteResult
           (
           new RouteValueDictionary(new
           {
               action = "Error",
               controller = "Error"
           }));

            }
        }
        else
        {
            context.Result = new RedirectToRouteResult
            (
            new RouteValueDictionary(new
            {
                action = "Error",
                controller = "Error"
            }));

        }
    }
}

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

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