![](/img/trans.png)
[英]Accessing Active Directory in ASP.net locally works fine. After deploying the same code on server gives Authorization-errors
[英]Integrating ASP.NET code to Active Directory or LDAP after deploying on Bluemix
我正在开发一个ASP.Net项目,需要在PaaS完成后部署,需要BlueMix (这不是我的选择,这是一个订单)。
另外我需要使用:
Active Directory或LDAP到用户身份验证和授权,与ASP.Net项目集成。
这里的问题是:
1.我发现只使用Java或Node.js集成到Active Directory或SSO服务,但在我的情况下,我使用的是ASP.Net
2.我想要一个解决方案,了解如何在Active Directory和ASP.Net应用程序之间的PaaS之上完成集成。
根据您使用的ADFS版本,您应该能够使用OAuth或OIDC中间件从ASP.NET Core应用程序进行连接(假设您使用的是ASP.NET Core,因为您使用的是Bluemix)。 如果您至少使用ADFS 3.0(Windows Server 2012+),则可以使用ASP.NET Core的通用OAuth中间件进行连接。
首先,创建配置文件以存储ADFS服务器设置,或修改现有配置文件(例如appsettings.json)。
示例“adfs-settings.json”文件:
{
"ADFS": {
"ClientId": "Your ClientId as set on ADFS server",
"ResourceUrl": "url of this application (ex: http://mywebapp.mybluemix.net)",
"ServerUrl": "url of ADFS (ex: https://dc.your.domain)"
}
}
如果为ADFS配置创建了新文件(例如“adfs-settings.json”),请将其添加到Startup.cs文件的构造函数中的Configuration
对象中。
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("adfs-settings.json");
Configuration = builder.Build();
}
在Startup.cs的Configure
方法中,创建一个OAuthOptions
对象:
var options = new OAuthOptions();
options.AutomaticChallenge = true;
options.AuthenticationScheme = "ADFS";
通过从Configuration
对象中读取此应用程序,指定在ADFS服务器上配置此应用程序时创建的ClientId
。 通用OAuth中间件还要求您在此处提供ClientSecret
,即使ADFS 3.0实际上未使用该值。
options.ClientId = Configuration["ADFS:ClientId"];
options.ClientSecret = "ADFS 3.0 does not support confidential client, but OAuth middleware requires it";
设置ADFS服务器将在您的应用程序中重定向到的回调URL。
options.CallbackPath = new PathString("/signin-adfs");
现在配置OAuthEvents
。 OnRedirectToAuthorizationEndpoint
定义当应用程序确定需要授权用户时传递给ADFS授权终结点的参数。 这将至少需要一个指向应用程序URL的resource
参数。 当ADFS服务器完成对客户端的授权并将包含声明数据的JWT令牌返回给您的应用程序时,将触发OnCreatingTicket
。 在此方法中,您需要处理向HttpContext
对象添加角色和声明。
options.Events = new OAuthEvents {
OnRedirectToAuthorizationEndpoint = context =>
{
var parameter = new Dictionary<string, string>
{
["resource"] = Configuration["ADFS:ResourceUrl"]
};
var query = QueryHelpers.AddQueryString(context.RedirectUri, parameter);
context.Response.Redirect(query);
return Task.CompletedTask;
},
OnCreatingTicket = context => {
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
JwtSecurityToken validatedToken = tokenHandler.ReadJwtToken(context.AccessToken);
IEnumerable<Claim> a = validatedToken.Claims;
foreach (var claim in a)
{
// role claim needs to be mapped to http://schemas.microsoft.com/ws/2008/06/identity/claims/role
// for IsInRole() to work properly
if (claim.Type == "role")
{
context.Identity.AddClaim(new Claim(ClaimTypes.Role, claim.Value));
}
else if (claim.Type == "unique_name")
{
// map name to Identity.Name
context.Identity.AddClaim(new Claim(context.Identity.NameClaimType, claim.Value));
}
else
{
// this is optional, if you want any other specific claims from Active Directory
// this will also include some information about the jwt token such as the issue
// and expire times
context.Identity.AddClaim(new Claim(claim.Type, claim.Value));
}
}
return Task.CompletedTask;
}
};
接下来,将ClaimsIssuer
设置为ADFS URL并将SignInScheme
设置为SignInScheme
, CookieAuthenticationDefaults.AuthenticationScheme
AuthorizationEndpoint
和TokenEndpoint
配置为ADFS服务器上的正确端点。
options.ClaimsIssuer = Configuration["ADFS:ServerUrl"];
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.AuthorizationEndpoint = Configuration["ADFS:ServerUrl"] + "/adfs/oauth2/authorize/";
options.TokenEndpoint = Configuration["ADFS:ServerUrl"] + "/adfs/oauth2/token/";
最后,使用您刚刚创建的选项添加OAuth中间件:
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOAuthAuthentication(options);
现在,您应该能够将[Authorize]
属性应用于需要使用ADFS进行授权的任何控制器或操作。 有关完整的示例应用程序,请参阅此GitHub存储库 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.