.Net Claims Auth - 无法设置当前主体

时间:2014-02-16 05:02:03

标签: c# asp.net asp.net-mvc-4 authentication claims-based-identity

我正在尝试使用基于声明的身份验证重新登录系统。

到目前为止,这么好

单步执行,它似乎正确评估用户名和密码并正确创建声明主体(包括添加身份验证类型以将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)。

我哪里错了?

一些想法:

  • 是因为我还没有设置SessionSecurityToken吗?
  • 我是否完全错过了有关线程或正确设置上下文的内容?
  • 由于UserLogin方法是在MVC中,我也尝试使用控制器上下文,但这似乎也不起作用。
  • 中间还有其他东西可能会弄乱这个吗?
    • 阅读:是否有一种简单的方法可以验证旧登录系统的某些部分是否未被遗留并且与我玩弄?

2 个答案:

答案 0 :(得分:8)

经过大量研究(并且涉及excellent Pluralsight Course by Dominick Baier),解决方案如下:

重叠的大步骤/问题

  • 我没有设置会话身份验证cookie,因此重定向被视为新请求,没有看到任何cookie,也没有设置主体。
  • 后来,当我使用会话身份验证管理器时,事实证明Cassini(VS的内置调试服务器)根本没有加载SessionAuthenticationManager
    • (IIS和IIS Express就这么做了。)

完整解决方案

一步一步(再次,大部分内容归功于Dominick的视频):

步骤1:向配置

添加身份服务
  • 右键单击您的项目,然后选择“添加引用...”
  • 在“框架”部分中,选择System.IdentityModel.Services
  • 将以下内容添加到您的web.config:

(下面的完整大纲,在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>

步骤2:添加会话身份验证管理器

(取决于该配置设置)

在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"/>

步骤3:删除Global.asax

中的PostAuthenticate方法

(不再需要因为SAM,它会检测到cookie;为什么在每次请求时运行它,如果你不需要,对吧?)

步骤4:设置Claims Transformation方法以设置身份验证cookie

在您的ClaimsAuthenticationManager中添加这些行(我的名为ClaimsTransformer)。我把它放在一个名为“EstablishSession”的单独方法中,它在我的校长已经被转换后接受了它:

private void EstablishSession(ClaimsPrincipal transformedPrincipal)
{
    var sessionToken = new SessionSecurityToken(transformedPrincipal, TimeSpan.FromHours(8));
    FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(sessionToken);
}

所以现在每当你转换声明时总会设置cookie,这是有道理的,因为如果用户成功通过身份验证,你只会转换声明。

第五步:撕掉你的头发......

...想知道为什么SessionAuthenticationManager始终为null。

说真的,一切似乎都有效,而且你的配置是正确的,但如果它不是每个都是空的,那就是它。单。时间。

步骤6:将调试Web服务器切换到IIS Express

啊,似乎Cassini(VS Debugger中的内置版)不能与SessionAuthenticationManager一起使用。

然而,IIS Express确实如此。将其切换到项目设置中的那个。

和Voila!

现在我有一个有效的页面。

答案 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;

}

相关问题