我正在尝试使用基于声明的身份验证重新登录系统。
单步执行,它似乎正确评估用户名和密码并正确创建声明主体(包括添加身份验证类型以将IsAuthenticated设置为true,per this SO question。)
不知何故,身份似乎没有在线上正确设置。结果,我被直接重定向回登录页面。
我在global.asax
中有以下内容:
private void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
var currentPrincipal = ClaimsPrincipal.Current;
var transformer = new ClaimsTransformer(); //My own custom transformer; code below.
var newPrincipal = transformer.Authenticate(string.Empty, currentPrincipal); // does the transformation
// as I understand, it is proper & recommnded to set both of these
Thread.CurrentPrincipal = newPrincipal;
HttpContext.Current.User = newPrincipal;
}
在我的登录控制器中,我对会员数据库进行了简单的测试。我在调试时验证了它有newCP
作为具有预期名称的有效,经过身份验证的标识。
[HttpPost]
[AllowAnonymous]
public ActionResult UserLogin(LoginViewModel viewModel)
{
var loginSuccess = Membership.ValidateUser(viewModel.UserName, viewModel.Password);
if (loginSuccess)
{
// CustomApplicationIdentity puts some identity-based logic into business domain terms and uses Claims underneath.
//Should have done it at the IPrincipal level, but instead I created the ToAuthenticatedStandardClaimsIdentity() which returns a new authenticated ClaimsIdentity.
var newIdentity = new CustomApplicationIdentity(viewModel.UserName);
var cp = new ClaimsPrincipal(newIdentity.ToAuthenticatedStandardClaimsIdentity());
var newCP = new ClaimsTransformer().Authenticate(string.Empty, cp);
System.Web.HttpContext.Current.User = newCP;
Thread.CurrentPrincipal = newCP;
if (!string.IsNullOrWhiteSpace(viewModel.ReturnUrl))
{
return Redirect(viewModel.ReturnUrl);
}
return RedirectToAction("Index", "Identity");
}
}
当它重定向到Action时,我看到它再次点击Application_PostAuthenticateRequest
,这很有道理。
然而,尽管先前设置了主体,但现在看来这是一个空主体(没有名称,IsAuthenticated设置为false)。
一些想法:
答案 0 :(得分:8)
经过大量研究(并且涉及excellent Pluralsight Course by Dominick Baier),解决方案如下:
一步一步(再次,大部分内容归功于Dominick的视频):
System.IdentityModel.Services
(下面的完整大纲,在web.config中插入该大纲中的两个部分):
<configuration>
<configSections>
<section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</configSections>
</configuration>
(取决于该配置设置)
在web.config的system.webServer
部分中,添加以下行:
<remove name="RoleManager"/> <!--Not needed anymore in my case -->
<add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
(不再需要因为SAM,它会检测到cookie;为什么在每次请求时运行它,如果你不需要,对吧?)
在您的ClaimsAuthenticationManager中添加这些行(我的名为ClaimsTransformer)。我把它放在一个名为“EstablishSession”的单独方法中,它在我的校长已经被转换后接受了它:
private void EstablishSession(ClaimsPrincipal transformedPrincipal)
{
var sessionToken = new SessionSecurityToken(transformedPrincipal, TimeSpan.FromHours(8));
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(sessionToken);
}
所以现在每当你转换声明时总会设置cookie,这是有道理的,因为如果用户成功通过身份验证,你只会转换声明。
...想知道为什么SessionAuthenticationManager始终为null。
说真的,一切似乎都有效,而且你的配置是正确的,但如果它不是每个都是空的,那就是它。单。时间。
啊,似乎Cassini(VS Debugger中的内置版)不能与SessionAuthenticationManager一起使用。
然而,IIS Express确实如此。将其切换到项目设置中的那个。
现在我有一个有效的页面。
答案 1 :(得分:-2)
我认为你需要实现SessionSecurityToken,或者在页面请求之间保持会话的东西。这是一种更自定义的方法:
public static int SetAuthCookie(this HttpResponseBase responseBase, User user, bool rememberMe)
{
// Initialize Session Ticket
var authTicket = new FormsAuthenticationTicket(1
, user.Email
, DateTime.Now
, DateTime.Now.AddHours(30)
, rememberMe
, JsonConvert.SerializeObject(new {
Email = user.Email,
FirstName = user.FirstName,
Id = user.Id
})
, FormsAuthentication.FormsCookiePath);
var encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
if (authTicket.IsPersistent)
authCookie.Expires = authTicket.Expiration;
responseBase.Cookies.Add(authCookie);
return encTicket.Length;
}
public static void VerifyAuthCookie(HttpContext context)
{
HttpCookie authCookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null)
return;
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket == null)
return;
if (authTicket.Expired)
return;
User user = !string.IsNullOrEmpty(authTicket.UserData) ? JsonConvert.DeserializeObject<User>(authTicket.UserData) : null;
if (user == null)
return;
// Create an Identity object
UserIdentity id = new UserIdentity(user, authTicket);
// This principal will flow throughout the request.
GenericPrincipal principal = new GenericPrincipal(id, new [] { "User" });
context.User = principal;
}