我有一个MVC intranet站点,它需要使用AD帐户进行身份验证。
我设置了ADFS3.0 (Win Server2012 R2),并遵循this设置了ADFS信任方。
This的另一篇文章介绍了Ws-Federation OWIN组件,我想使用它。它提到了如何连接到Azure AD,但没有关于ADFS的内容。
我尝试设置配置属性"MetadataAddress“和"Wtrealm”来匹配我在ADFS中配置的内容,但在运行时得到一个错误:
A default value for SignInAsAuthenticationType was not found in IAppBuilder Properties.
This can happen if your authentication middleware are added in the wrong order, or if one is missing.我正在寻找正确的方法来删除这个错误
发布于 2014-04-24 23:21:09
是啊..我也遇到了同样的问题。只需执行以下操作,它就会起作用:
app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType );
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
});发布于 2014-12-12 21:43:01
我已经尝试了一段时间了,特别要感谢Lars Kemmann和Tratcher,我相信可以接受的方法如下:
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(
new CookieAuthenticationOptions { }
);
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
Wtrealm = ConfigurationManager.AppSettings["ida:Wtrealm"],
MetadataAddress = ConfigurationManager.AppSettings["ida:FedMetadataURI"]
}
);您将默认身份验证类型配置为“Cookie身份验证”以使WsFederation正常工作,这似乎有悖于直觉,但是这些实际上只是用于标识每个中间件的字符串(例如,这允许您多次注册相同类型的中间件),它们的评估如下:
CookieAuthenticationDefaults.AuthenticationType = "Cookies"WsFederationAuthenticationDefaults.AuthenticationType =“联合”这里发生的事情是,我们告诉OWIN,标记为“Cookie”的中间件应默认用于验证请求,然后添加CookieAuthentication中间件(默认情况下,这是来自CookieAuthenticationDefaults.AuthenticationType值的标记为“Cookie”,因此我们不必编写任何额外的代码来设置它),最后我们添加FederationAuthentication中间件(标记为WsFederationAuthenticationDefaults.AuthenticationType -即“联合”),我的理解是联合中间件利用Cookie中间件来管理与身份验证相关的Cookie。
剩下要做的就是配置你的应用程序在你选择的时间调用中间件,这可以通过许多方法来实现,其中一些如下所示:
[Authorize]属性返回HTTP401 responseIAuthenticationManager的Challenge方法(传入您的联合中间件的标签)当我问这个问题here时,Lars用一个简洁的例子回答了如何为所有请求请求身份验证,然后我将其捆绑到OWIN管道中,如下所示:
app.Use(
(context, continuation) =>
{
if (
(context.Authentication.User != null) &&
(context.Authentication.User.Identity != null) &&
(context.Authentication.User.Identity.IsAuthenticated)
)
{
return continuation();
}
else
{
context.Authentication.Challenge(WsFederationAuthenticationDefaults.AuthenticationType);
return Task.Delay(0);
}
}
);请注意,在上面的第一个示例中,为了便于维护,我将Wtrealm和MetadataAddress值移动到了我的配置文件中,它们只是简单的应用程序设置:
<appSettings>
<add key="ida:Wtrealm" value="[app-uri]" />
<add key="ida:FedMetadataURI" value="https://[adfs-server]/federationmetadata/2007-06/federationmetadata.xml" />
</appSettings>我希望这能帮到你。
发布于 2014-08-23 01:46:04
实际上,您只是遗漏了通常在UseCookieAuthentication方法调用之前的这一行。
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);在您的情况下,它将是
app.UseExternalSignInCookie(WsFederationAuthenticationDefaults.AuthenticationType);这是调用UseExternalSignInCookie(...)时实际执行的内容,externalAuthenticationType是作为字符串参数传入的内容。
app.SetDefaultSignInAsAuthenticationType(externalAuthenticationType);
CookieAuthenticationOptions options = new CookieAuthenticationOptions();
options.AuthenticationType = externalAuthenticationType;
options.AuthenticationMode = AuthenticationMode.Passive;
options.CookieName = ".AspNet." + externalAuthenticationType;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5.0);
app.UseCookieAuthentication(options);因此,如果您所有的设置都是AuthenticationType,那么您可以安全地调用UseExternalSignInCookie,因为它为您做了这件事。
https://stackoverflow.com/questions/23262471
复制相似问题