【问题标题】:ASP.NET External Authentication Services IntegrationASP.NET 外部身份验证服务集成
【发布时间】:2013-10-17 06:55:02
【问题描述】:

我的 ASP.NET webapp 将受到第三方代理 (SM) 的保护。 SM 将拦截对 webapp 的每个调用,将用户验证为有效的系统用户,添加一些标头信息 ex username 并将其重定向到我的 webapp。然后我需要验证该用户是我网站的活跃用户。

目前我通过在Global.asax.cs 文件中实现Application_AuthenticateRequest 方法来验证用户。我有一个自定义成员资格提供程序,其 ValidateUser 方法检查用户是否存在于我的数据库的用户表中。

只是想获得 cmets,如果这是一个好方法。

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        //if user is not already authenticated
        if (HttpContext.Current.User == null)
        {

            var smcred = ParseAuthorizationHeader(Request);
            //validate that this user is a active user in the database via Custom Membership 
            if (Membership.ValidateUser(smcred.SMUser, null))
            {
                //set cookie so the user is not re-validated on every call.
                FormsAuthentication.SetAuthCookie(smcred.SMUser, false);
                var identity = new GenericIdentity(smcred.SMUser);
                string[] roles = null;//todo-implement role provider Roles.Provider.GetRolesForUser(smcred.SMUser);
                var principal = new GenericPrincipal(identity, roles);

                Thread.CurrentPrincipal = principal;
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.User = principal;
                }
            }

        }
    }

    protected virtual SMCredentials ParseAuthorizationHeader(HttpRequest request)
    {
        string authHeader = null;
        var smcredential = new SMCredentials();
    //here is where I will parse the request header for relevant tokens ex username

        //return smcredential;
        //mockup below for username henry
        return new SMCredentials() { SMUser = "henry", FirstName = "", LastName = "", EmailAddr = "" };

    }

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 authentication asp.net-web-api authorization


    【解决方案1】:

    我会采用 Attribute 方法以使其更像 MVC。它还可以为您提供更大的灵活性,您可能为不同的控制器/操作拥有不同的成员资格提供程序。

    【讨论】:

    • 您的意思是实现自定义 AuthorizeAttribute 吗?如果是这样,那么从我读到的 AuthenticateRequest 发生在授权调用之前。
    • 是的,自定义 AuthorizeAttribute 是我要做的。事件处理程序和自定义属性都将帮助您实现您所描述的,但 AuthorizeAttribute 将为您提供更多的灵活性和单元可测试性。
    • 为了保持身份验证(谁是用户)和授权(该用户有权访问什么),我决定在事件处理程序中对用户进行身份验证,而我执行角色授权(是用户活动?是用户管理员?)具有授权属性。这有意义吗
    猜你喜欢
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2013-02-22
    • 2017-09-28
    • 2011-08-03
    • 1970-01-01
    相关资源
    最近更新 更多