ASP.NET MVC登录客户端/ ASP.NET WebAPI身份验证/授权服务器分离

时间:2014-05-23 02:46:26

标签: c# asp.net authentication asp.net-web-api claims-based-identity

我试图将ASP.Net MVC登录客户端与将使用令牌承载的身份验证服务器分开,并将包含所有身份验证业务逻辑。这两件事情分为2个不同的webroles

我使用Identity 2.0的当前实现完成了很多工作。

UserManager和UserStore位于AuthServer中,登录客户端对userId一无所知。只有UserName。

目前,要在客户端项目中生成用户的声明,我使用此实现:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync()
    { 
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var claims = new List<Claim>()
       {
            new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", this.UserName),
            new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", this.UserName),
            new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity"),
       };
        var claimsIdentity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

        //var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here

        return claimsIdentity;
    }

正如你所看到的,我在这里摆脱了经理,但我担心我会错过一些安全功能,例如Startup.Auth.cs中的CookieAuthenticationProvider,他们使用了用户的SecurityStamp。

Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync())
            }

我试图调用AuthServer的GenerateUserIdentityAsync,它调用manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie)函数,但是我需要用户名ID ...客户端一无所知。

有什么想法吗? CreateIdentityAsync的代码似乎不是开源的。

感谢并为这篇长篇文章感到抱歉。

1 个答案:

答案 0 :(得分:1)

我刚刚调用了AuthenticationServer Api,它由授权头中的令牌承载进行身份验证。 AuthServerProxy已传递从cookie中获取的承载令牌。

public async Task<ClaimsIdentity> GenerateUserIdentityAsync()
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType

        var userIdentity = await AuthServerProxy.CreateIdentityAsync(DefaultAuthenticationTypes.ApplicationCookie);

        // Add custom user claims here

        return userIdentity;
    }    

我使用授权标头(bearer token)调用Authentication Server

[Route("CreateIdentity")]
public async Task<IHttpActionResult> CreateIdentity([FromBody] string authenticationType)
    {
        ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
        ClaimsIdentity userIdentity = await user.GenerateUserIdentityAsync(UserManager, authenticationType);
        return Ok(userIdentity);
    }

因此,登录客户端Web角色中不再有UserManager。 这可以被清理得更多,但至少,SoC受到尊重,EF从客户端消失。

相关问题