在控制器中设置用户角色?
我需要能够在控制器中手动授权我的用户。
我从 AD 获取身份验证,然后在我的控制器中获取身份验证,
我想上地图 我从 AD 获得的用户 ID 到我的应用程序的内部用户 ID。
从 UserRole 表中获取 userId,然后将其设置到控制器中,但是,
不知道控制器中的角色如何设置?
我尝试在我的家庭控制器中执行此操作:
HttpContext.User = new System.Security.Principal.GenericPrincipal(User.Identity, roleName);
roleName 设置为“Admin”,但这似乎不起作用,因为它总是失败授权。
请帮忙?...
I need to be able to manually authorize my users in my controller.
I get my authentication from an AD, and then in my controller,
I want to map up
the userID I get from the AD, to my application's internal userID.
Grab the userId from the UserRole table, and then set it in the controller, however,
I don't know how to set the role in the controller?
I've tried doing this in my home controller:
HttpContext.User = new System.Security.Principal.GenericPrincipal(User.Identity, roleName);
roleName is set to "Admin", but this doesn't seem to work as it always fails authorization.
Help please?....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您在控制器方法中使用
[Authorize]
,这将在操作方法之前运行,因此不会到达您必须设置角色名称的代码 - 它需要在调用控制器之前设置。将这样的方法添加到您的 Global.asax 中:
Assuming you are using
[Authorize]
in your controller methods, this will run before the action method and therefore will not reach the code you have to set the role name - it needs to be set before the controller is called.Add a method like this to your Global.asax: