简体   繁体   English

硬编码ASP.NET MVC的角色(数据库外部)并添加Windows身份验证的用户

[英]Hardcode a role (outside database) for ASP.NET MVC and adding a Windows Authenticated Users

How would I go about hardcoding a role in ASP.NET MVC within the program itself, rather than through a database, for authentication (and then how could I add people to this role)? 我将如何在程序本身内而不是通过数据库对ASP.NET MVC中的角色进行硬编码以进行身份​​验证(然后如何为该角色添加人员)?

What I'm trying to do is have 3 roles: Progammer, DatabaseAdministrator, and SystemsAdministrator. 我想做的是扮演3个角色:Progammer,DatabaseAdministrator和SystemsAdministrator。 I'd like to be able to add people to those roles (also hardcoded), and then authenticate people based on role, rather than username. 我希望能够将人员添加到这些角色中(也进行硬编码),然后根据角色而不是用户名对人员进行身份验证。 Right now, I am authenticating people like this: 现在,我正在对这样的人进行身份验证:

If (@User.Identity.Name == "DOMAIN\\first.m.last")

What I want to be able to do is: 我想要做的是:

If (@User.Identity.Role == "ROLENAME")

However, I only have three roles and 8 people; 但是,我只有三个角色和8个人。 I do not want to have to create a table in my database for them. 我不想在我的数据库中为他们创建一个表。 So I'd create the role DatabaseAdministrator, and then add three people into as the string "DOMAIN\\first.m.last". 因此,我将创建角色DatabaseAdministrator,然后将三个人添加为字符串“ DOMAIN \\ first.m.last”。 Then, I could have the action populate the view based on their rolename. 然后,我可以根据他们的角色名来填充视图。

Everything I've seen so far has you do it through the database. 到目前为止,我所看到的所有内容都是通过数据库完成的。 Would appreciate any help. 将不胜感激。 Thanks! 谢谢!

I am assuming that you are using Windows Authentication. 我假设您正在使用Windows身份验证。

With that said you can easily authorize based on the users Active Directory Groups. 这样,您可以轻松地基于用户Active Directory组进行授权。 This will keep you out of the database and all you have to do is add a user to an AD group if the users change. 这将使您脱离数据库,并且您所要做的就是在用户更改后将用户添加到AD组。

Then you can authorize on the controller or action with this annotation. 然后,您可以使用此批注授权控制器或操作。

[Authorize(Roles = @"DOMAIN\ADGroup")]

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

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