Csharp/C#教程:如何使用证书而不是密码进行身份validation?分享


如何使用证书而不是密码进行身份validation?

我有一个小型的C#MVC5应用程序,我正在构建并准备添加用户安全模块。 (之前我刚刚创建了一个用于测试角色的会话变量)但是,我的安全需求不适合我见过的任何预构建的安全模块,即SimpleMembership等。

以下是我的情况和需求的摘要:

实施这样的计划的最佳方法是什么? 我开始做的是构建三个表 – Users,Roles和UserRoles – 而Users表包含(string)ClientCertificate的副本。 进入的用户将通过拉取Request.ClientCertificate并查找匹配的用户记录,然后从UserRoles获取角色列表来对应用程序进行身份validation。 用户对象将存储在包含用户和角色信息的会话中,并且属性将在控制器上用于通过要求某些角色来控制访问。

(我们有另一个使用这种基本方法的应用程序,但它是Linux上的J2EE所以我不能只重用它的代码)

然而,我也开始阅读有关IIdentity和IPrincipal的内容,但我不清楚这是否是我可以使用的东西。 显然,我更喜欢使用由专家设计的安全模型。 那么我应该使用inheritance自IIdentity和IPrincipal的自定义类来构建我的身份validation系统吗? 或者我应该使用其他方法吗?

完全有可能像SimpleMembership这样的东西可以定制,以满足我的需求,但如果是这样,我不知道它。

感谢您的建议,非常感谢。

您也可以将Microsoft的Identity用于此类高级方案。 身份如此模块化,您可以将任何数据存储用于您想要的任何架构。 身份validation的密码不是必需的,您可以实现自己的方案。 考虑这个简单的例子:

// imaging this action is called after user authorized by remote server public ActionResoult Login() { // imaging this method gets authorized certificate string // from Request.ClientCertificate or even a remote server var userCer=_certificateManager.GetCertificateString(); // you have own user manager which returns user by certificate string var user=_myUserManager.GetUserByCertificate(userCer); if(user!=null) { // user is valid, going to authenticate user for my App var ident = new ClaimsIdentity( new[] { // since userCer is unique for each user we could easily // use it as a claim. If not use user table ID new Claim("Certificate", userCer), // adding following 2 claim just for supporting default antiforgery provider new Claim(ClaimTypes.NameIdentifier, userCer), new Claim("https://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "https://www.w3.org/2001/XMLSchema#string"), // an optional claim you could omit this new Claim(ClaimTypes.Name, user.Name), // populate assigned user's role form your DB // and add each one as a claim new Claim(ClaimTypes.Role, user.Roles[0].Name), new Claim(ClaimTypes.Role, user.Roles[1].Name), // and so on }, DefaultAuthenticationTypes.ApplicationCookie); // Identity is sign in user based on claim don't matter // how you generated it Identity take care of it HttpContext.GetOwinContext().Authentication.SignIn( new AuthenticationProperties { IsPersistent = false }, ident); // auth is succeed, without needing any password just claim based return RedirectToAction("MyAction"); } // invalid certificate ModelState.AddModelError("", "We could not authorize you :("); return View(); } 

正如您所看到的,我们授权用户和填充的角色,而不依赖于用户名,密码和任何数据存储,因为我们使用了自己的用户管理器。

一些用法示例:

 [Authorize] public ActionResult Foo() { } // since we injected user roles to Identity we could do this as well [Authorize(Roles="admin")] public ActionResult Foo() { // since we injected our authentication mechanism to Identity pipeline // we have access current user principal by calling also // HttpContext.User } 

这是一个简单的示例,您可以实现自定义方案extend IIdenity 。 阅读我的其他类似的答案,这样和更多的例子,看看如何通过Claims几乎所有的一切。

您也可以浏览和下载基于令牌的身份validation样本仓库作为一个简单的工作示例。

根据它的声音,用户已经过身份validation,因此在控制器中,您只需访问控制器inheritance的Controller基类上的User属性即可。 这是属性返回一个IIPrincipal ,它有一个名为Identity的属性,它返回一个IIdentity

因此,例如,如果您想要在控制器中检索当前登录用户的用户名,则可以访问User.Identity.Name或者如果您想确保用户已登录,则可以检查User.Identity.IsAuthenticated

您绝对不应该从客户端证书中提取任何内容,本质上服务器应该已经对用户进行了身份validation,您并不关心它是通过证书而不是用户名/密码来完成的。

上述就是C#学习教程:如何使用证书而不是密码进行身份validation?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1016365.html

(0)
上一篇 2021年12月31日
下一篇 2021年12月31日

精彩推荐