如何使用服务主体名称(SSO)验证用户并提供角色基本访问权限 [英] How to validate a user using service principal name (SSO) and provide role base access

查看:92
本文介绍了如何使用服务主体名称(SSO)验证用户并提供角色基本访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在Single Sign On和Azure App Proxy上绝对是新手。



根据要求,我们需要为我们的门户网站实施单点登录(使用服务主体名称),并通过Azure环境中托管的Azure应用程序代理进行访问。



截至目前,我已使用set spn命令在Dev环境中为主机名配置了SPN。



我不知道如何从这里继续前进。我需要使用此SPN验证用户(不确定它是否可能???)。我想我需要使用C#代码阅读此SPN,但这将如何帮助我验证用户???



此外,在验证用户身份后,我需要提供他们基于角色访问菜单项。



任何帮助和代码将不胜感激。如果需要进一步的详细信息,请告诉我。



注意:我正在使用C#和MVC开发此应用程序。



谢谢

Rahul



我的尝试:



我尝试过以下链接但无法达到要求:

使用.NET中的主体和标识对象洞察安全模型 [ ^ ]

推荐答案

服务主体用于在Active Directory中保存应用程序连接信息,以便应用程序可以知道从网络中的任何位置连接到何处,而无需管理本地存储的设置。它不用于身份验证或授权。



要在MVC中执行身份验证或授权,您需要以下内容:

The service principal is used for saving application connection information in Active Directory so that an application can know where to connect from anywhere in your network to without having to manage locally stored settings. It is not used for authentication or authorization.

To perform authenrication or authorization in MVC, you'll need something along these lines:
using System.DirectoryServices.AccountManagement;
namespace MyWebApp.Controllers
{
 public class MyLandingPage : Controller
 {
  public ActionResult Index()
  {
   if (!User.Identity.IsAuthenticated) // test if the user is connecting with Windows Authentication
   {
    return new RedirectToAction("AutheticationFailure", "ErrorController");
   }

   var adContext = new PricipalContext(ContextType.Domain); // this tells it use your network AD

   Pricipal user = Principal.FindByIdentity(adContext, User.Identity.Name));

   if (user == null)
   {
     // redirect to not authenticated page
   }

   GroupPrincipal group = GroupPricipal.FindByIdentity(context, "MyWebAppUserGroup"); // use the samAccountName or UPN

   if (group == null)
   {
     // redirect to OOPS page as somebody deleted your security group or you typed it wrong
   }

   if (user.IsMemberOf(group))
   {
     // at this point, user is authenticated and passed authorization check.  Okay to proceed.
   }
   else
   {
     // redirect to Not Authorized page
   }
  }
 }
}



您需要添加对DirectoryServices.AccountManagement dll的引用。



***编辑

等等,我只是将Service PRicipals与Service Connections混淆了。让我读一下并调整代码。



***编辑REDUX

如果我理解你要做什么,您已配置SPN以了解连接的位置。你没有使用SPN进行授权,所以上面的代码仍然有效。



附加点:

Web应用程序是无状态的,所以你会需要通过每次Web调用来授权用户。

对于基于权限的菜单,您可以确定用户权限并将其权限级别添加到传递给视图的模型中,并在Razor视图引擎中使用它根据个人用户包含/排除菜单。


You'll need to add the reference to the DirectoryServices.AccountManagement dll.

***EDIT
Wait, I just confused Service Pricipals with Service Connections. Let me read up a bit and adjust the code.

***EDIT REDUX
If I am understanding what you are trying to do, you have configured an SPN to know where to connect. You don't use SPNs for authorization, so the above code still stands.

Additional Points:
Web applications are stateless so you will need to authorize the users with each web call.
For privilege based menus, you can determine user permissions and add their permission level into a model that you pass to the view and use that in the Razor view engine to include/exclude menus based on individual users.


这篇关于如何使用服务主体名称(SSO)验证用户并提供角色基本访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆