问题描述
我通过 LDAP 与旧的 Active Directive 集成,其中一些用户将被查找。 如果我找到了该用户,它应该具有访问权限 - 没有其他要求。
我有一个特定页面,我对其设置了限制,因此只有来自 MyGroup
的用户才能看到。当我访问该站点时,我被重定向到我的登录页面并得到一个不错的 ReturnUrl
,但我得到一个 401
返回该页面。
EPiServer 拒绝让我进入,尽管我可以看到我确实得到了我的用户。
我如何告诉 EPiServer,给定的用户被允许访问?
我创建了自己的 LoginController
,它调用了这样的方法:
public AuthenticationResult SignInAsLdapUser(string username,string password)
{
var principalContext = new PrincipalContext(/*...*/);
isAuthenticated = principalContext.ValidateCredentials(username,password);
if (!isAuthenticated)
return;
var userPrincipal = UserPrincipal.FindByIdentity(principalContext,username);
//Attempt to force additional claims on user
var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie,ClaimsIdentity.DefaultNameClaimType,ClaimsIdentity.DefaultRoleClaimType);
identity.AddClaim(new Claim(ClaimTypes.Role,"MyGroup")); //Group I am trying to add
HttpContext.Current.GetowinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.Current.GetowinContext().Authentication.SignIn(new AuthenticationProperties
{
IsPersistent = true,ExpiresUtc = DateTimeOffset.Now.AddMinutes(30),AllowRefresh = true
},identity);
return new AuthenticationResult();
}
在我的 web.config
中,我指定了组 MyGroup
:
<virtualRoles addClaims="true">
<providers>
...
<add name="CmsEditors" type="EPiServer.Security.MappedRole,EPiServer.Framework" roles="MyGroup,..." mode="Any"/>
...
</providers>
</virtualRoles>
在我的 startup.cs
中,我指定了内置身份和 cookie:
public void Configuration(IAppBuilder app)
{
app.AddCmsAspNetIdentity<ApplicationUser>(); //OWIN from EPiServer
app.UseCookieAuthentication(new CookieAuthenticationoptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,AuthenticationMode = AuthenticationMode.Active,CookieSecure = CookieSecureOption.Always,LoginPath = new PathString("/Login"),CookieName = "MyCookie",ExpireTimeSpan = TimeSpan.FromDays(7),ReturnUrlParameter = "ReturnUrl",Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator
.OnValidateIdentity<ApplicationUserManager<ApplicationUser>,ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),regenerateIdentity: (manager,user) => manager.GenerateUserIdentityAsync(user)),OnApplyRedirect = context => context.Response.Redirect(context.RedirectUri),OnResponseSignIn = context => context.Response.Redirect(GetReturnUrl()),}
});
app.UseStageMarker(Pipelinestage.Authenticate);
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)