加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET MVC登录客户端/ ASP.NET WebAPI身份验证/授权服务

发布时间:2020-12-15 22:04:05 所属栏目:百科 来源:网络整理
导读:我正在尝试将ASP.Net MVC登录客户端与将使用令牌承载的身份验证服务器分开,并将包含所有身份验证业务逻辑.这两件事情分为2个不同的webroles 我已经完成了使用Identity 2.0的当前实现. UserManager和UserStore位于AuthServer中,登录客户端对userId一无所知.只
我正在尝试将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",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的代码似乎不是开源的.

谢谢和抱歉长篇文章.

解决方法

我刚刚调用了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;
    }

我使用授权标头(承载令牌)调用认证服务器

[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从客户端消失.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读