首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在中间件中,User.IsInRole()始终为假

在中间件中,User.IsInRole()始终为假
EN

Stack Overflow用户
提问于 2018-05-18 17:17:28
回答 1查看 249关注 0票数 0

我有一个简单的中间件类:

    public class RouterMiddleware {
    private readonly RequestDelegate _next;

    public RouterMiddleware(RequestDelegate next)
    {
        this._next = next;
    }

    public async Task Invoke(HttpContext context)
    {

        if (!context.User.IsInRole("Guest"))
        {
            await _next.Invoke(context);
            return;
        }

        if (context.Request.Path.StartsWithSegments("/home/") 
            || context.Request.Path.StartsWithSegments("/api/profile/")
        {
                await _next.Invoke(context);
        }
        else
        {
            context.Response.Redirect("/home/");
            return;
        }
    }

}

在任何情况下,对于任何角色,context.User.IsInRole("Guest")都会返回false。有没有办法检查中间件类中的用户角色?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-18 19:35:13

问题出在Startup.cs上。中间件的顺序是:

        app.UseMiddleware<Middleware.RouterMiddleware>();
        app.UseAuthentication();

但必须是:

        app.UseAuthentication();
        app.UseMiddleware<Middleware.RouterMiddleware>();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50407880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档