首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MVC管道中的身份验证失败

MVC管道中的身份验证失败
EN

Stack Overflow用户
提问于 2014-01-02 00:01:34
回答 2查看 258关注 0票数 0

由于FormsAuthentication模块在处理OnAuthenticateRequest的自定义http模块之前触发,我很好奇是否可以根据自己的标准取消或使表单身份验证无效。

基本上,我有一个用户登录的进程。在那之后,他们会得到一个令牌。在后续请求触发表单身份验证后,我将得到令牌。然后,我要做的是验证令牌是否在我们的后端服务器上过期。如果它过期了,我需要做些什么,这样他们才能被强制重新登录。我的想法是在我的OnAuthenticateRequest处理程序中做一些事情,稍后将在管道中获得,并强制重定向回登录页面或其他什么。这有可能吗?

EN

Stack Overflow用户

发布于 2014-01-02 00:08:13

在ASP.NET MVC应用程序中,为了处理自定义的身份验证和授权,人们通常编写自定义的Authorize属性。它们不处理任何OnAuthenticateRequest事件。这是老生常谈。顺便说一句,如果您要进行一些自定义的令牌身份验证,为什么还要关心表单身份验证呢?为什么不换掉它呢?

所以:

代码语言:javascript
复制
public class MyAuthorizeAttribute: AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        string token = GetTokenFromHttpContext(httpContext);
        if (IsTokenValid(token))
        {
            // The user has provided a valid token => you need to set the User property
            // Obviously here based on the token value you already know which is the
            // associated user and potential roles, so you could do additional checks

            var identity = new GenericIdentity("john.smith");
            var user = new GenericPrincipal(identity, new string[0]);
            httpContext.User = user;

            return true;
        }

        // Notice that here we are never calling the base AuthorizeCore method
        // but you could call it if needed 

        return false;
    }

    private string GetTokenFromHttpContext(HttpContextBase httpContext)
    {
        // TODO: you know what to do here: get the token from the current HTTP Context
        // depending on how the client passed it in: HTTP request header, query string parameter, cookie, ...
        throw new NotImplementedException();
    }

    private bool IsTokenValid(string token)
    {
        // TODO: You know what to do here: go validate the token
        throw new NotImplementedException();
    }
}

现在剩下的就是用这个自定义属性来修饰你的控制器/动作,而不是使用默认的属性:

代码语言:javascript
复制
[MyAuthorize]
public ActionResult SomeAction()
{
    // if you get that far you could use the this.User property
    // to access the currently authenticated user
    ...
}
票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20870400

复制
相关文章

相似问题

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