1

我有一个 CAS 服务器设置为身份验证类型的 ASP.NET MVC 站点。我还有一个单独的数据库,其中包含一个用户表和一个角色表(用户与一个或多个角色相关)。只有当用户名同时在用户表和 CAS 系统中时,用户才能登录系统。我有这个解决方案工作。

我的问题是我现在需要在 User.IsAuthenticated 上使用某种形式的触发器,这样我就可以跟踪当前用户(来自我的数据库),而我不可能尝试允许跟踪已注销的用户。我一直在想的是我需要将用户添加到 HttpContext 但如果 CAS 会话超时或用户注销,我不确定如何触发用户的清除。

我还希望有一些功能,例如 User.IsInRole(再次使用我的数据库,而不是 ASP.NET),但我不确定如何实现这一点。我想如果我可以成功地将用户添加到 HttpContext 中,那么 IsInRole 方法将只是一个 User.Roles.Contains(string role) 方法,但是如果我希望,例如,使用具有DataAnnotation [授权(角色=“ExampleRole”)]。

我查看了诸如如何为 ASP.NET MVC 2 创建自定义成员资格提供程序?但这对我不起作用(可能与我使用 CAS 身份验证有关?)

任何指导或背景阅读将不胜感激,因为我真的不确定我应该从哪里开始。我已经阅读了 GenericPrinciple、IPrinciple 和 IIdentity,但我正在努力了解如何将它们应用到我当前的项目中。

4

2 回答 2

0

Ended up with a custom Authorise Attribute that uses the CAS logon to check the user exists in my database. It also checks the roles of that user. I also used a static class to save the current user in the session with a logout method that abandons the session when the user logs out.

于 2012-04-23T10:56:34.863 回答
-1

我为你准备了一个二人组。此链接很好地解释了如何用您自己的对象替换 HttpContext 用户:http: //bradygaster.com/custom-authentication-with-mvc-3.0

他的方法使用 MVC 过滤器,但您也可以在 Global.asax 文件中捕获 Authentication 事件。将表单系统与您自己的实现一起使用可能很简单,这取决于您在做什么,但归结为在您自己的逻辑中调用 FormsAuthentication.SetAuthCookie 和 .SignOut。

   public static void FormsLogin(this User user, bool persist)
    {
        FormsAuthentication.SetAuthCookie(user.DisplayName, persist);
        user.AddHistory("Login event.", HistoryType.Login, "SYSTEM");
        Users.OnUserLogin(user);
        SetLastActivity(user);
    }

    public static void FormsLogout(this User user)
    {
        FormsAuthentication.SignOut();
    }

Lastly, once you've got the login stuff working out, you can use your own more complex permission system by making a custom Auth Attribute. I remember piecing this together from some other answers and articles but I can't seem to find the sources at the moment, I will try and edit with sources for credit where it's due, if I find them. For now, all I can offer is this gist which offers up one of the attributes I use: https://gist.github.com/1959509

Keep in mind the only really relevant part there is the override of OnAuthorization, which does the actual work.

于 2012-03-02T16:38:12.620 回答