简体   繁体   English

ASP.NET MVC身份验证在会话中存储Active Directory组

[英]Asp.net mvc authentication storing Active Directory groups in a session

I am currently working on an external web application. 我目前正在使用外部Web应用程序。 I am starting to work on the security. 我开始致力于安全性。 For example this is a course registration system, and there are admins, students, and instructors. 例如,这是一个课程注册系统,并且有管理员,学生和讲师。 Only the admins should be able to add instructors so I need to lock that part down. 只有管​​理员应该可以添加讲师,因此我需要锁定该部分。 I have figured out how to authenticate with active directory and bring back the users profile. 我已经弄清楚了如何通过活动目录进行身份验证并带回用户个人资料。 My thought was to store the users Active Directory groups in a session and use that to determine what the user can see. 我的想法是将用户的Active Directory组存储在会话中,并使用该组来确定用户可以看到的内容。 My problem is I can't seem to figure out how to store the AD group in the session. 我的问题是我似乎无法弄清楚如何在会话中存储AD组。 For instance let's say the admin is in a group called SYSTEM-ADMIN. 例如,假设管理员在一个名为SYSTEM-ADMIN的组中。 How do I put that in a session to use it for securing my site. 如何在会话中使用它来保护我的网站。 I have never had to do this before and can't seem to find much on how to add things like this to a session. 我以前从来没有做过此事,似乎也找不到如何在会话中添加类似内容的东西。 I believe it would involve using the group to give the user a particular role and store the role in a session? 我相信这将涉及使用组为用户赋予特定角色并将角色存储在会话中? Any help/resources would be appreciated. 任何帮助/资源将不胜感激。 Thanks. 谢谢。

I needed to do a few different things then I expected First create an enum: 我需要做一些不同的事情,然后我期望首先创建一个枚举:

public enum Role
{
                Admin,
                Teacher,
                Student
}

Create a class: 创建一个类:

public class SessionObject
{
                public Role UserRole { get; set; }
}

Create the session object and set the Role property, then assign that to the session: 创建会话对象并设置Role属性,然后将其分配给会话:

SessionObject oSessionObject = new SessionObject { Role = Role.Admin };
Session[“SessionObject”] = oSessionObject;

if (Session[“SessionObject”] != null)
{
                vm.SessionObject = ((SessionObject) Session[“SessionObject”]);
}

Then you just check the vm.SessionObject.Role to see what role the user is. 然后,您只需检查vm.SessionObject.Role即可查看用户的角色。

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

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