ABP集成WIF实现单点登录
ABP集成WIF实现单点登录
参考
ojlovecd写了三篇关于WIF文章。
使用WIF的一些开源示例。
https://github.com/obhita/PROCenter/tree/master/ProCenter.LocalSTS/STS
https://github.com/primaryobjects/SingleSignOn
https://github.com/shinpou/Windows-Identity-Foundation-Tutorial/tree/master/Learning%20SSO
WSFederation and SAML library for Java based web applications
https://github.com/auth10/auth10-java
WIF配置过程中的一些问题
1、WIF授权
MVC项目中使用[Authorize],注解方式鉴权。
webform项目中使用配置文件方式鉴权。
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
2、密钥集合不存在
Exception: System.Security.Cryptography.CryptographicException
Message: 密钥集不存在。
解决方法:
C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys这个目录授权的时候出错了,一直报这个错误
“将安全信息应用到以下对象时发生错误 拒绝访问”,为ereryone用户分配完全控制权限,密钥集的错误就解决了。
3、部署时,出现
用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法
解决方法:
IIS 应用程序池--选中你网站的所配置的应用程序池--右键 选择 “高级配置” --将“加载用户配置文件” 设置为True 。
4、thumbprint值
配置文件中<add thumbprint="xxxxxxxxxx" name="sts" />的值,使用IIS服务器证书中的指纹值。复制粘贴会带一些特殊字符,还是手敲吧!
在ABP中集成
User.Identity && User.Identity.IsAuthenticated并不依赖Session。
Web.config文件设置或者浏览器请求时候删除ASP.NET_SessionId只要保留.AspNet.ApplicationCookie存在请求身份一样能通过验证。
<system.web>
<sessionState mode="Off" />
</system.web>
RP站
RP OnAuthentication鉴权:
protected override void OnAuthentication(AuthenticationContext filterContext)
{
if (filterContext.HttpContext == null)
throw new ArgumentNullException("httpContext");
IPrincipal user = filterContext.HttpContext.User; bool isFederation = false;
bool isApp = false;
IEnumerable<ClaimsIdentity> cl = ((ClaimsPrincipal)User).Identities;
if (cl != null && cl.Count() > )
{
foreach (var item in cl)
{
if (item.AuthenticationType.Equals("Federation") && item.IsAuthenticated)
{
isFederation = true;
}
else if (item.AuthenticationType.Equals("ApplicationCookie") && item.IsAuthenticated)
{
isApp = true;
if (item.Claims.Where(p => p.Type == "SSO-Login").Count() == )
isFederation = true;
}
}
}
if (!isFederation || !isApp)
filterContext.Result = new RedirectResult("/Account/Login"); if (!user.Identity.IsAuthenticated || !AbpSession.UserId.HasValue)
filterContext.Result = new RedirectResult("/Account/Login");
}
RP登录:
public async Task<ActionResult> Login(string userNameOrEmailAddress = "", string returnUrl = "", string successMessage = "")
{
bool isFederation = false, isApp = false;
var identities = ((ClaimsPrincipal)User).Identities;
if(identities!=null && identities.Count() > )
{
var federation = identities.Where(p => p.AuthenticationType == "Federation").FirstOrDefault();
if (federation != null)
isFederation = federation.IsAuthenticated; var applicationCookie = identities.Where(p => p.AuthenticationType == "ApplicationCookie").FirstOrDefault();
if (applicationCookie != null)
isApp = applicationCookie.IsAuthenticated;
}
if (!isFederation)
{
FederatedAuthentication.WSFederationAuthenticationModule.SignIn(null);
return null;
}
//创建本地abpsession
if (!isApp)
{
string name = string.Empty, role = string.Empty;
ClaimsIdentity claims = User.Identity as ClaimsIdentity;
if (claims != null)
{
var list = claims.Claims.ToList();
if (list != null && list.Count > )
{
var cName = claims.Claims.Where(p => p.Type == ClaimTypes.Name).FirstOrDefault();
if (cName != null)
name = cName.Value;
var cRole = claims.Claims.Where(p => p.Type == ClaimTypes.Role).FirstOrDefault();
if (cRole != null)
role = cRole.Value; var cEmail = claims.Claims.Where(p => p.Type == ClaimTypes.Email).FirstOrDefault();
if (cEmail != null)
name = cEmail.Value; if (name.Contains("\\"))
name = name.Replace("\\", "");
if (string.IsNullOrEmpty(role))
role = ConfigHelper.GetAppSetting("UserRole");
}
}
var user = _userIdentity.GetUserByName(name);
if (user == null)
{
CreateOrUpdateUserInput input = new CreateOrUpdateUserInput();
input.User = new UserEditDto();
input.User.Name = name;
input.User.Surname = input.User.Name;
input.User.UserName = input.User.Name;
input.User.Password = Infrastructure.Authorization.Users.User.CreateRandomPassword();
input.User.EmailAddress = name + "@xx.com";
input.AssignedRoleNames = new string[] { role }; user = await _userIdentity.CreateUserAsync(input);
}
await SignInAsync(user);
}
return RedirectToAction("Index", "Home");
}
RP 登出:
public ActionResult Logout()
{
IEnumerable<ClaimsIdentity> cis = ((ClaimsPrincipal)User).Identities;
if (cis != null && cis.Count() > )
{
foreach (var item in cis)
{
if (item.AuthenticationType.Equals("Federation"))
{
var authModule = FederatedAuthentication.WSFederationAuthenticationModule;
WSFederationAuthenticationModule.FederatedSignOut(new Uri(authModule.Issuer + "/Account/Logout"), new Uri(authModule.Reply));
}
else if (item.AuthenticationType.Equals("ApplicationCookie"))
{
_authenticationManager.SignOutAll();
}
}
}
return RedirectToAction("Login");
}
STS认证成功后回调RP(host:www.test.com)会传入如下信息
将信息.AspNet.ApplicationCookie、FedAuth、FedAuth1复制后使用其它浏览器写入这三个Cookie刷新可正常验证用户信息。
FedAuth、FedAuth1是WIF STS给的认证信息。

如果访问的子站点(www.test.com)没有通过STS将认证(FedAuth、FedAuth1)信息回写到子站点的Cookie中,此时使用其它已认证的子站点(FedAuth、FedAuth1)信息模拟到www.test.com站点是无法识别的。

WIF认证服务端:
CustomSecurityTokenService.cs
protected override ClaimsIdentity GetOutputClaimsIdentity(ClaimsPrincipal principal, RequestSecurityToken request, Scope scope)
{
//WIF返回Claims中信息,里面了包含自己想要的声明
ClaimsIdentity outgoingIdentity = new ClaimsIdentity();
var claims = principal.Claims.ToList();
foreach(var item in claims)
{
if(item.Type != "AspNet.Identity.SecurityStamp" && item.Type != ClaimTypes.NameIdentifier)
{
outgoingIdentity.AddClaim(new Claim(item.Type, item.Value));
}
}
SingleSignOnManager.RegisterRP(scope.AppliesToAddress);
return outgoingIdentity;
}
将Claims信息调用RP站管道信息带入到RP站:
[UnitOfWork]
public ActionResult Index()
{
FederatedPassiveSecurityTokenServiceOperations.ProcessRequest(
System.Web.HttpContext.Current.Request,
User as ClaimsPrincipal,
CustomSecurityTokenServiceConfiguration.Current.CreateSecurityTokenService(),
System.Web.HttpContext.Current.Response);
var loginInfo = AsyncHelper.RunSync(() => _sessionAppService.GetCurrentLoginInformations());
return View(loginInfo);
}
服务端登录的用户信息。

ABP集成WIF实现单点登录的更多相关文章
- abp集成IdentityServer4和单点登录
在abp开发的系统后,需要使用这个系统作单点登录,及其他项目登录账号依靠abp开发的系统.在官方文档上只找到作为登录服务Identity Server Integration,但是host项目却无法使 ...
- 使用WIF实现单点登录Part III —— 正式实战 -摘自网络
经过前两篇文章,估计大家对WIF已经有比较充分的认识了,估计大家在经过了枯燥的理论以后,肯定是摩拳擦掌赶紧付诸于行动了.没办法,咱们程序员就是这个毛病.那好吧,我也不那么多废话了,直接进入正题吧. 我 ...
- 使用WIF实现单点登录Part II —— Windows Identity Foundation基本原理
在上一篇文章中,我们已经使用WIF构建了一个基于MVC4的简单的身份验证程序,在这篇文章里,我们将探讨一下到底什么是WIF,以及它的工作原理.然后在下一篇文章开始,我们将实际操作,实现单点登录功能. ...
- 使用WIF实现单点登录Part II —— Windows Identity Foundation基本原理 -摘自网络
在上一篇文章中,我们已经使用WIF构建了一个基于MVC4的简单的身份验证程序,在这篇文章里,我们将探讨一下到底什么是WIF,以及它的工作原理.然后在下一篇文章开始,我们将实际操作,实现单点登录功能. ...
- 使用WIF实现单点登录Part I——Windows Identity Foundation介绍及环境搭建 -摘自网络
上个月有一个星期的时间都在研究asp.net mvc统一身份验证及单点登录的实现.经过了一番的探索,最终决定使用微软的Windows Identity Foundation.但是这东西用的人貌似不多, ...
- 使用WIF实现单点登录Part IV —— 常见问题
InvalidOperationException: ID1073: 尝试使用 ProtectedData API 解密 Cookie 时出现 CryptographicException (有关详细 ...
- 使用WIF实现单点登录Part III —— 正式实战
我们接下来的demo将包括以下的工程: SiteA —— 基于.net framework 4.5的MVC 4程序,使用WIF 4.5的SDK,第一个RP SiteB —— 基于.net framew ...
- spring security集成cas实现单点登录
spring security集成cas 0.配置本地ssl连接 操作记录如下: =====================1.创建证书文件thekeystore ,并导出为thekeystore.c ...
- Spring Security 集成CAS实现单点登录
参考:http://elim.iteye.com/blog/2270446 众所周知,Cas是对单点登录的一种实现.本文假设读者已经了解了Cas的原理及其使用,这些内容在本文将不会讨论.Cas有Ser ...
随机推荐
- nginx负载均衡的5种策略
nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个. nginx的upstre ...
- 小论文matlab作图技巧
小论文matlab作图技巧 编辑->复制选项 编辑->图形属性 图中右击->字型 编辑->复制图片,即可. 效果: 宽:5.9高: 7.91
- C++编译器详解(一)
C/C++编译器-cl.exe的命令选项 和在IDE中编译相比,命令行模式编译速度更快,并可以避免被IDE产生的一些附加信息所干扰,本文将介绍微软C/C++编译器命令行模式设定和用法. 1.设置环境变 ...
- MFC模块状态(一)
先看一个例子: 1.创建一个动态链接到MFC DLL的规则DLL,其内部包含一个对话框资源.指定该对话框ID如下: #define IDD_DLL_DIALOG 2000 ...
- 关于git的ssh permission denied原因汇总
SSH关于公钥认证Permission denied (publickey,gssapi-with-mic的问题 http://h2appy.blog.51cto.com/609721/1112797 ...
- POJ2431--Expedition(优先队列)
Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...
- 20155326 2017-2018-1 《信息安全系统设计基础》课下加分项mypwd实现
20155326 2017-2018-1 <信息安全系统设计基础>课下加分项mypwd实现 pwd命令能做什么 在虚拟机中输入pwd查看其返回的是什么 通过上图得知pwd命令用来显示目录. ...
- Hdu2389 Rain on your Parade (HK二分图最大匹配)
Rain on your Parade Problem Description You’re giving a party in the garden of your villa by the sea ...
- lowbit(x)
int Lowbit(int x) { return x&(-x); } lowbit当中x,-x,补码,反码,傻傻分不清楚.我们先看看两个二进制数相减的问题 两个二进制数相减的相关问题 两个 ...
- JBoss 系列四十九:JBoss 7/WildFly 中端口使用列表
JBoss 7中端口使用列表 JBoss 7中所有配置都在一个文件中(standaone*.xml, domain.xml),和之前的JBoss相比JBoss 7用到的端口变少,我们将以表格的形式列出 ...