加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Azure上的联合身份验证

发布时间:2020-12-15 18:50:19 所属栏目:asp.Net 来源:网络整理
导读:我使用WIF(.net 4.5)和Azure Active目录进行身份验证。该网站将坐在Azure上。 一切工作作为预期的本地,但是当我把它放在azure我得到的错误: The data protection operation was unsuccessful. This may have been caused by not having the user profile l
我使用WIF(.net 4.5)和Azure Active目录进行身份验证。该网站将坐在Azure上。

一切工作作为预期的本地,但是当我把它放在azure我得到的错误:

The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread’s user context,which may be the case when the thread is impersonating.

我明白这是因为应用程序不能使用DAPI,所以我需要切换到使用MAC保护我的应用程序。

本地我添加到我的webconfig: –

<securityTokenHandlers>
    <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler,System.IdentityModel,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />
    <add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler,System.IdentityModel.Services,PublicKeyToken=b77a5c561934e089" />
  </securityTokenHandlers>

如在documentation中推荐的,我添加了一个静态机器密钥,但我找不到任何建议围绕密钥长度 – 所以我假设256。

然而这个配置只是给出这个错误:

[CryptographicException: Error occurred during a cryptographic operation.]
System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.HomogenizeErrors(Func`2 func,Byte[] input) +115
System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.Unprotect(Byte[] protectedData) +59
System.Web.Security.MachineKey.Unprotect(ICryptoServiceProvider cryptoServiceProvider,Byte[] protectedData,String[] purposes) +62
System.Web.Security.MachineKey.Unprotect(Byte[] protectedData,String[] purposes) +122
System.IdentityModel.Services.MachineKeyTransform.Decode(Byte[] encoded) +161
System.IdentityModel.Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte[] cookie,Boolean outbound) +123
System.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader,SecurityTokenResolver tokenResolver) +575
System.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token,SecurityTokenResolver tokenResolver) +76
System.IdentityModel.Services.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie) +833
System.IdentityModel.Services.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken) +186
System.IdentityModel.Services.SessionAuthenticationModule.OnAuthenticateRequest(Object sender,EventArgs eventArgs) +210
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously) +69

我删除了machinekey部分incase我没有指定正确格式化的键,但错误不会消失。

什么战斗WIF已经!

解决方法

如果没有在配置中指定machineKey,Azure会添加一个。但是,如果您创建新版本的应用程序并使用VIP切换将其部署到Azure,Azure会在Staging中生成新机器部署的键(假设您的第一个部署是生产)。 (VIP交换是部署新版本然后在生产和分段之间交换虚拟IP地址的好机制)。

所以基本上一个解决方案是让Azure生成密钥,但VIP开关后你有问题回来。为了避免它,你可以捕获Global.asax中的CryptographicException在Application_Error处理程序,像这样:

// Be sure to reference System.IdentityModel.Services
// and include using System.IdentityModel.Services; 
// at the start of your class
protected void Application_Error(object sender,EventArgs e)
{
    var error = Server.GetLastError();
    var cryptoEx = error as CryptographicException;
    if (cryptoEx != null)
    {
        FederatedAuthentication.WSFederationAuthenticationModule.SignOut();
        Server.ClearError();
    }
}

SignOut()方法导致cookie被删除。

编辑:生成machineKey的更新信息,如@anjdreas所示。

另一个解决方案是生成machineKey,可以使用IIS管理器来做到这一点,详情参见Easiest way to generate MachineKey。如果将相同的密钥放入Azure Web角色中的所有Web应用程序中,则Azure部署过程将不会替换它。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读