我必须为我现有的 asp.net Web 应用程序使用 saml2 进行 SSO 身份验证。
我正在使用 Sustainsys.Saml2.Owin 示例来执行此操作。
身份提供者是 Azure ADFS ( https://sts.windows.net/TENANTID )
我已经配置了启动文件。它加载元数据文件和证书。
在我的登录页面中,如果未通过身份验证,我将面临挑战。
它已成功重定向到登录页面,但登录后请求从未获得身份验证。在回复 URL 中,我们得到error=access_denied
[Request.IsAuthenticated 或 owinContext.Authentication.User.Identity.IsAuthenticated 均未设置为 true]
因此,它不断挑战多次,并因错误请求而出错。
我做错了什么?Owin/Sustainsys 的哪个模块可以设置 IsAuthenticated 状态?
*一个 Saml2。cookie [Saml2.DAeP63c***UTX0h***_***] 在登录 Microsoft 后随请求一起传递 [ https://login.microsoftonline.com/TENANTID/saml2]
启动.cs文件
public void ConfigureAuth(IAppBuilder appBuilder)
{
try
{
appBuilder.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions());
appBuilder.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
appBuilder.UseSaml2Authentication(CreateSaml2Options());
}
catch (Exception exp)
{
}
}
private Saml2AuthenticationOptions CreateSaml2Options()
{
try
{
var spOptions = CreateSPOptions();
var Saml2AuthOptions = new Saml2AuthenticationOptions(false)
{
SPOptions = spOptions,
Notifications = new Saml2Notifications(),
};
var idp = new IdentityProvider(new EntityId(authority), spOptions)
{
MetadataLocation = metadataLocation,
Binding = Saml2BindingType.HttpRedirect
};
idp.SigningKeys.AddConfiguredKey(
new X509Certificate2(certificateLocation));
Saml2AuthOptions.IdentityProviders.Add(idp);
return Saml2AuthOptions;
}
catch (Exception exp)
{
}
}
private SPOptions CreateSPOptions()
{
try
{
var engAus = "en-AU";
var organization = new Organization();
var spOptions = new SPOptions
{
EntityId = new EntityId(ApplicationId),
ReturnUrl = new Uri(redirectUrl),
Organization = organization,
};
return spOptions;
}
catch (Exception exp)
{
}
}
登录.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IOwinContext owinContext = HttpContext.Current.GetOwinContext();
//if (Request.IsAuthenticated)
if (owinContext.Authentication.User != null &&
owinContext.Authentication.User.Identity != null &&
owinContext.Authentication.User.Identity.IsAuthenticated)
{
//Authenticated
string name = owinContext.Authentication.User.Identity.Name;
}
else
{
var authenticationTypes = owinContext.Authentication.GetAuthenticationTypes().Select(d => d.AuthenticationType).ToArray();
owinContext.Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, authenticationTypes);
}
}
}