如何解决强制要求用户
我通过 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 举报,一经查实,本站将立刻删除。