使用IdentityServer对特定用户进行身份验证并将其分配给特定客户端

3
我正在为几个客户端使用IdentityServerV3来验证用户身份。我在配置IdentityServer时遇到问题,需要使特定的user能够登录到特定的客户端。
让我们看下面的场景:
我有2个客户端->client1client2。 我有3个用户->user1user2user3user1user2仅能访问client1。而user3只能访问client2
当我尝试使用user3登录到client1时,身份验证服务器会成功进行身份验证,这是我不想要的。
public static class Clients
{
    public static IEnumerable<Client> Get()
    {
        return new[]
        {
            new Client 
            {
                Enabled = true,
                ClientName = "MVC Client",
                ClientId = "mvc",
                Flow = Flows.Hybrid,

                RedirectUris = new List<string>
                {
                    "https://localhost:44319/"
                }
            },
            new Client 
            {
                Enabled = true,
                ClientName = "MVC Client 2",
                ClientId = "mvc2",
                Flow = Flows.Hybrid,

                RedirectUris = new List<string>
                {
                    "https://localhost:44319/"
                }
            }
        };
    }
}

用户是:

public static class Users
{
    public static List<InMemoryUser> Get()
    {
        return new List<InMemoryUser>
        {
            new InMemoryUser
            {
                Username = "bob",
                Password = "secret",
                Subject = "1",

                Claims = new[]
                {
                    new Claim(Constants.ClaimTypes.GivenName, "Bob"),
                    new Claim(Constants.ClaimTypes.FamilyName, "Smith")
                }
            },
            new InMemoryUser
            {
                Username = "rob",
                Password = "secret",
                Subject = "2",

                Claims = new[]
                {
                    new Claim(Constants.ClaimTypes.GivenName, "Rob"),
                    new Claim(Constants.ClaimTypes.FamilyName, "Thompson")
                }
            },
            new InMemoryUser
            {
                Username = "Jerry",
                Password = "secret",
                Subject = "3",

                Claims = new[]
                {
                    new Claim(Constants.ClaimTypes.GivenName, "Jerry"),
                    new Claim(Constants.ClaimTypes.FamilyName, "Smith")
                }
            }
        };
    }
}

现在,OWIN启动类是:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Map("/identity", idsrvApp =>
            {
                idsrvApp.UseIdentityServer(new IdentityServerOptions
                {
                    SiteName = "Embedded IdentityServer",
                    SigningCertificate = LoadCertificate(),

                    Factory = InMemoryFactory.Create(
                        users  : Users.Get(),
                        clients: Clients.Get(),
                        scopes : StandardScopes.All)
                });
            });
    }

    X509Certificate2 LoadCertificate()
    {
        return new X509Certificate2(
            string.Format(@"{0}\bin\identityServer\idsrv3test.pfx", AppDomain.CurrentDomain.BaseDirectory), "idsrv3test");
    }
}

我不明白IdentityServer如何知道哪个用户属于哪个客户端。我的情况是否不受IdentityServer的支持,或者我有所遗漏。 更新:
    public class LocalRegistrationUserService : UserServiceBase
    {
        public class CustomUser
        {
            public string Subject { get; set; }
            public string Username { get; set; }
            public string Password { get; set; }
            public List<Claim> Claims { get; set; }
        }

        public static List<CustomUser> Users = new List<CustomUser>();

        public string ClientId { get; set; }

        public override Task AuthenticateLocalAsync(LocalAuthenticationContext context)
        {
            var user = Users.SingleOrDefault(x => x.Username == context.UserName && x.Password == context.Password);
            if (user != null)
            {
                context.AuthenticateResult = new AuthenticateResult(user.Subject, user.Username);
            }

            return Task.FromResult(0);
        }
}

在注册用户时,我将其重定向到Identity Server Host中的控制器。

 public class LocalRegistrationController : Controller
    {
        [Route("core/localregistration")]
        [HttpGet]
        public ActionResult Index(string signin)
        {
            return View();
        }

        [Route("core/localregistration")]
        [HttpPost]
        public ActionResult Index(string signin, LocalRegistrationModel model)
        {
            var ctx = Request.GetOwinContext();
            if (ModelState.IsValid)
            {
                var user = new LocalRegistrationUserService.CustomUser
                {
                    Username = model.Username, 
                    Password = model.Password, 
                    Subject = Guid.NewGuid().ToString(),
                    Claims = new List<Claim>()
                };
                LocalRegistrationUserService.Users.Add(user);
                user.Claims.Add(new Claim(Constants.ClaimTypes.GivenName, model.First));
                user.Claims.Add(new Claim(Constants.ClaimTypes.FamilyName, model.Last));

                return Redirect("~/core/" + Constants.RoutePaths.Login + "?signin=" + signin);
            }

            return View();
        }
    }

startup.cs

 app.Map("/core", coreApp =>
            {
                var factory = new IdentityServerServiceFactory()
                    .UseInMemoryClients(Clients.Get())
                    .UseInMemoryScopes(Scopes.Get());

                // different examples of custom user services
                //var userService = new RegisterFirstExternalRegistrationUserService();
                //var userService = new ExternalRegistrationUserService();

                var userService = new LocalRegistrationUserService();

                // note: for the sample this registration is a singletone (not what you want in production probably)
                factory.UserService = new Registration<IUserService>(resolver => userService);
                factory.ViewService = new Registration<IViewService, CustomViewService>();
                var options = new IdentityServerOptions
                {
                    SiteName = "Identity Server 3",

                    SigningCertificate = Certificate.Get(),
                    Factory = factory,

                    AuthenticationOptions = new AuthenticationOptions
                    {
                        IdentityProviders = ConfigureAdditionalIdentityProviders,
                        LoginPageLinks = new LoginPageLink[] { 
                            new LoginPageLink{
                                Text = "Register",
                               // Href = "~/externalregistration",
                                Href = "~/localregistration",
                                //Href = "localregistration"
                            },
                             new LoginPageLink{
                                Text = "Forgot Password?",
                                Href = "~/forgotpassword",
                                //Href = "localregistration"
                            }
                        }
                    },

                    EventsOptions = new EventsOptions
                    {
                        RaiseSuccessEvents = true,
                        RaiseErrorEvents = true,
                        RaiseFailureEvents = true,
                        RaiseInformationEvents = true
                    }
                };

                coreApp.UseIdentityServer(options);
            });

我需要了解在注册时如何最好地将用户映射到客户端,并确保该用户只能登录到该客户端而不是任何其他客户端,即使该用户的用户名密码对于所有客户端都相同。


你解决了吗?能分享一下你的解决方案吗? - Nate
1个回答

4
如果您提供一个名为application_access的自定义声明来去填写您的id_token,并且该值是用户可以访问的应用程序/客户端(其中可能有多个),那么会怎样呢?您可以在IUserService实现中添加此自定义声明,并以与映射其他用户特定声明相同的方式进行映射。

然后,在您的客户端中,检查该特定声明,并检查已登录用户是否具有该特定客户端的特定值的声明。

https://github.com/IdentityModel/Thinktecture.IdentityModel.45/blob/master/IdentityModel/Thinktecture.IdentityModel/Authorization/WebApi/ScopeAttribute.cs

(备注:RequireScope用于访问令牌和API,但您可以得到一般思路)


谢谢你的建议,但我还没有完全理解。在通过一个额外的链接“注册”注册用户时,我需要将用户映射到用户重定向到“登录”表单的“客户端”,并且该用户只能为该特定客户端进行身份验证。 - Venkata Dorisala
我更新了我的问题,以便更好地向您展示我正在尝试做什么。希望有所帮助。谢谢。 - Venkata Dorisala
然后只需创建一个动态注册页面,该页面接受查询字符串参数“application”,并从每个应用程序链接到它。 - John Korsnes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接