我正在使用Asp.net Identity(OWIN)构建ASP.NET MVC 5网站,并希望支持传统的用户名/密码身份验证以及针对Azure Active Directory的身份验证。此应用程序不需要针对Microsoft ID(Live ID),Facebook,Twitter或任何其他外部提供程序进行身份验证。我找到的最接近的SO问题就是这个问题:How to do both Azure Active Directory Single Sign On and Forms Authentications on ASP.NET MVC
我查看了使用“个人用户帐户”选项创建项目时创建的示例以及VS 2015中的“工作和学校帐户”选项。我的身份验证工作正常;只有当我尝试将它们结合起来时才会遇到问题。
在我的Startup_Auth.cs文件中,我正在配置OWIN,如下所示:
# starting the data stream from external process
IO.popen("./dump1090") do |data|
block = ""
# created sinatra server thread
t1 = Thread.new do
set :port, 8080
set :environment, :production
get '/aircrafts' do
return_message = {}
if !Aircraft::All.first.nil?
return_message[:status] == 'success'
return_message[:aircrafts] = message_maker
else
return_message[:status] = 'sorry - something went wrong'
return_message[:aircrafts] = []
end
return_message.to_json
end
end
# parsing the data in main thread -- the process
# I want to run alongside the server (parse_block updates Aircraft::All)
while line = data.gets
if line.to_s.split('').first == '*'
parse_block(block)
puts block
Aircraft::All.reject { |aircraft| Time.now.to_f - aircraft.contact_time > 30 }
block = ""
end
block += line.to_s
end
end
此配置适用于密码身份验证,但不适用于AAD身份验证。要启用AAD身份验证,我需要注释掉设置AuthenticationType
的行 public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
//app.UseCookieAuthentication(new CookieAuthenticationOptions { });
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
LoginPath = new PathString("/account/sign-in")
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = false,
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
SecurityTokenValidated = (context) =>
{
return Task.FromResult(0);
},
AuthorizationCodeReceived = (context) =>
{
return Task.FromResult(0);
},
AuthenticationFailed = (context) =>
{
context.OwinContext.Response.Redirect("/Home/Error");
context.HandleResponse(); // Suppress the exception
return Task.FromResult(0);
}
}
}
);
}
或者,只设置CookieAuthentication没有值。
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
我猜想有一个相对简单的方法,并会欣赏一些关于从哪里开始寻找的想法。
答案 0 :(得分:3)
我搜索了微软的例子。所有这些看起来都像是你的解决方案。看这里:
另一个例子是here WindowsAzureActiveDirectoryBearerAuthenticationOptions
答案 1 :(得分:2)
就在最近,ASP.NET团队的Damian Edwards开源了他们的community standup website on github。他们正在使用Azure AD,所以我希望它有助于朝着正确的方向发展,遗憾的是我没有任何Azure AD体验。
这里也是他们谈论它的youtube video of the standup,我想有一些技巧和暗示你可以使用它们。
答案 2 :(得分:0)
我意识到这是一个老问题。我可能希望做类似的事情,但可能更像ASP.Net身份验证到多个Azure AD租户。我在这句话中找到了这个Integrating Azure AD into ASP.NET Core:
...然后利用OnTokenValidated通知来实现您自己的发布者验证逻辑,具体取决于您要支持的租户(任何租户,Microsoft帐户+ Azure AD的特定列表,单个Azure AD,仅Microsoft帐户等)。 ..
让我相信那里的示例代码可能是这个混合身份验证方案的关键。