CAS custom role provider 冷不防 2022-05-21 11:21 199阅读 0赞 Monday, March 9, 2015 by Chris Sherman The [CAS authentication service][] is a single sign-on solution for web services used by a number of universities. When authenticating with CAS, the server has the option of embedding a list of user roles inside the encrypted authentication cookie. In this tutorial, I’ll explain how to write a custom role provider in ASP.NET MVC applications to extract the roles passed from the authentication server and integrate them with ASP.NET role authorization. If you don’t already have your app configured to use CAS authentication, check out my post on [getting started with the .NET CAS Client][]. ### Create the CAS Role Provider ### To integrate with ASP.NET web security, we need a class that implements the abstract class RoleProvider. Note that in the example below, I leave some of the methods as not implemented since all are not necessary to get started authorizing with CAS roles. I've named my class CasRoleProvider and placed it inside a folder named DataAccess. On my authorization server, the roles are passed under a property named roleAttributeName. This property may differ on your server, so be sure to check with your systems administrator and change the constant at the top of the class accordingly. using DotNetCasClient; using DotNetCasClient.Security; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Configuration.Provider; using System.Linq; using System.Web.Security; namespace SurplusPrototype.DataAccess { public class CasRoleProvider : RoleProvider { public const string ROLE_ATTRIBUTE_NAME = "roleAttributeName"; private readonly static IList EMPTY_LIST = new List(0).AsReadOnly(); private string roleAttribute; public override void Initialize(string name, NameValueCollection config) { if (config == null) { throw new ArgumentNullException("config"); } // Assign the provider a default name if it doesn't have one if (String.IsNullOrEmpty(name)) { name = "CasAssertionRoleProvider"; } base.Initialize(name, config); roleAttribute = config[ROLE_ATTRIBUTE_NAME]; if (roleAttribute == null) { throw new ProviderException(ROLE_ATTRIBUTE_NAME + " is required but has not been provided."); } if (roleAttribute == string.Empty) { throw new ProviderException(ROLE_ATTRIBUTE_NAME + " roleAttribute must be non-empty string."); } } public override void AddUsersToRoles(string[] usernames, string[] roleNames) { throw new NotImplementedException(); } public override string ApplicationName { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public override void CreateRole(string roleName) { throw new NotImplementedException(); } public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { throw new NotImplementedException(); } public override string[] FindUsersInRole(string roleName, string usernameToMatch) { throw new NotImplementedException(); } public override string[] GetAllRoles() { IList roles = GetCurrentUserRoles(); if (roles is Array) { return (string[])roles; } string[] roleArray = new string[roles.Count]; for (int i = 0; i < roles.Count; i++) { roleArray[i] = roles[i]; } return roleArray; } public override string[] GetRolesForUser(string username) { if (CasAuthentication.CurrentPrincipal.Identity.Name != username) { throw new ProviderException("Cannot fetch roles for user other than that of current context."); } return GetAllRoles(); } public override string[] GetUsersInRole(string roleName) { throw new NotImplementedException(); } public override bool IsUserInRole(string username, string roleName) { if (CasAuthentication.CurrentPrincipal.Identity.Name != username) { throw new ProviderException("Cannot fetch roles for user other than that of current context."); } return GetCurrentUserRoles().Count > 0; } public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { throw new NotImplementedException(); } public override bool RoleExists(string roleName) { throw new NotImplementedException(); } private IList GetCurrentUserRoles() { ICasPrincipal principal = CasAuthentication.CurrentPrincipal; if (principal == null) { return EMPTY_LIST; } HashSet roles = new HashSet(principal.Assertion.Attributes[roleAttribute]); if (roles == null) { roles = new HashSet(EMPTY_LIST); } return roles.ToList(); } } } ### Configure role authorization ### With the role provider created, we now need to configure the app to use the class for authorization. Open your Web.config file and add the following configuration under system.web. This will be below the forms authentication configuration. <configuration> <system.web> <!-- CAS Configuration --> <roleManager defaultProvider="CasRoleProvider" enabled="true" cacheRolesInCookie="true"> <providers> <clear /> <add name="CasRoleProvider" type="SurplusPrototype.DataAccess.CasRoleProvider" roleAttributeName="virginiaTechAffiliation" /> </providers> </roleManager> <!-- /CAS Configuration --> </system.web> </configuration> ### Force requests to authorize ### The final step is to protect the controllers with the ASP.NET authorization attribute. This will force the application into the authentication pipeline, authorizing only those users that have the specified role. Under App\_Start, open FilterConfig.cs and add a filter like the one below. Your role will be specific to your organization. filters.Add(new System.Web.Mvc.AuthorizeAttribute() \{ Roles = "VT-EMPLOYEE" \}); [CAS authentication service]: https://wiki.jasig.org/display/CAS/Home [getting started with the .NET CAS Client]: http://shermandigital.com/blog/integrate-cas-with-aspnet-applications/
相关 ansible--roles使用 一、ansible--roles使用 ansible在之前说明了playbook的使用,接下来介绍roles的使用(因剧本太多且杂乱,不在一一写出,请见谅) 1.N... 红太狼/ 2024年04月20日 09:22/ 0 赞/ 108 阅读
相关 role="navigation"> 这是一段HTML代码,它定义了一个导航栏的布局。其中包含了一个"navbar-header" div块和一个"navbar-brand"链接。这个链接具有"page-scrol 墨蓝/ 2024年03月26日 10:22/ 0 赞/ 68 阅读
相关 role属性:html中role的作用 > 这个role属性之前也没有注意过,下面为转载了解内容 role 是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明。 通常这种 矫情吗;*/ 2023年06月22日 04:26/ 0 赞/ 10 阅读
相关 Your Customer Is Not Your Customer  Your Customer Is Not Your Customer Eben Hewitt AS you WoRK in REquiREMEnTS MEETin ﹏ヽ暗。殇╰゛Y/ 2022年08月04日 15:33/ 0 赞/ 204 阅读
相关 CAS custom role provider Monday, March 9, 2015 by Chris Sherman The [CAS authentication service][] is a single s 冷不防/ 2022年05月21日 11:21/ 0 赞/ 200 阅读
相关 Tomcat默认role Tomcat默认role \\1.\\tomcat-users.xml,tomcat默认有四种角色 <?xml version="1.0" encoding=" 缺乏、安全感/ 2022年05月20日 05:38/ 0 赞/ 230 阅读
相关 Bootstrap role aria-* Intro `aria` Accesible Rich Internet Application,可访问的富Internet应用程序。 HTML5 针对HTML标签增加 末蓝、/ 2022年01月30日 12:13/ 0 赞/ 211 阅读
相关 ansible之role 注意三台都需要联网 我的是80.100、80.101、80.102 我是在80.100上操作的其他两台只要开着就行了不需要操作 yum install -y 今天药忘吃喽~/ 2022年01月13日 00:53/ 0 赞/ 251 阅读
相关 custom layer function CPos(x, y) { this.x = x; this.y = y; } functi àì夳堔傛蜴生んèń/ 2021年12月15日 10:47/ 0 赞/ 271 阅读
相关 Spring-Custom之spring-custom-web Spring-Custom之spring-custom-web > Spring-Custom是基于springboot开发的一些组件,自定义的一些规则规范开发过程. Bertha 。/ 2021年11月05日 08:32/ 0 赞/ 293 阅读
还没有评论,来说两句吧...