c# - 使用 Windows 帐户授权

标签 c# windows windows-store-apps

在我的 Windows 应用商店应用程序 (c#) 中,我有自己的授权机制:

  1. 用户输入他们的账户名/密码并将其发送到服务器。
  2. 服务器生成唯一 token 并将其返回给用户。
  3. 对于所有下一个请求,用户都使用此 token 。

现在我正在尝试仅使用 Windows 帐户进行授权。
MSDN provide UserInformation 类,我可以获得用户帐户名称用户域名。但我觉得这对我的授权方案来说还不够。

还有方法GetSessionInitiationProtocolUriAsync 看起来很有趣,但我不知道如何正确使用这样的Uri 进行授权。

如何在我的应用程序中使用 Windows 帐户进行授权?
注意:我对两种情况都感兴趣:用户是否在域内。

谢谢。

最佳答案

有很多方法可以实现这个,但是如果你想保持简单并拥有这个过程,你可以实现你自己的身份验证方法,在成功的身份验证后你可以从他们的密码和 secret 盐中构建一个哈希值,这个哈希值可以被返回作为 cookie 等发送给用户,您可以使用它来验证之后的每个请求。

关于授权,您可以实现自己的或使用链接到本地​​机器组或事件目录的基于角色的提供程序,方法是使用下面的类或仅使用普通的旧 RoleProviders .

您可以使用下面描述的方法或使用 Authentication 来实现您自己的身份验证方法和 ASP.Net 的授权提供程序(如果您的服务器在 .net 上运行)。基本上是 Asp.Net Membership and role Providers .但是,下面详述的方法也将允许您访问和修改角色以及有关用户的其他信息。

在 .Net 3.5+ 中有一个名为 System.DirectoryServices.AccountManagement 的新命名空间.

来自 MSDN 的片段

The System.DirectoryServices.AccountManagement namespace provides uniform access and manipulation of user, computer, and group security principals across the multiple principal stores: Active Directory Domain Services (AD DS), Active Directory Lightweight Directory Services (AD LDS), and Machine SAM (MSAM).

System.DirectoryServices.AccountManagement manages directory objects independent of the System.DirectoryServices namespace. Managed directory services applications can take advantage of the AccountManagement API to simplify management of user, computer and group principals. Solutions that previously required intricate knowledge of the store or lengthy code, such as finding all groups to which a user belongs, are accomplished in a few lines of code with the AccountManagement API.

您可以使用以下代码轻松验证 AD 上的用户凭据:

 bool valid = false;
 using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
 {
     valid = context.ValidateCredentials( username, password );
 }

如果您想使用本地计算机帐户进行验证,您也可以更改构造函数:

new PrincipalContext(ContextType.Machine)

您可以查看其他选项的文档,此外,这还可以让您从商店获得各种信息,例如成员(member)资格等。

新的命名空间是微软试图简化 DirectoryServices 的尝试,我认为这是成功的,但如果你想要更多的控制,你可以使用 DirectoryServices 类,但这会增加解决方案的复杂性。

如果您需要更多信息或者您认为这不是您要找的东西,我希望这对您有所帮助,请告诉我,我会努力改进答案。

关于c# - 使用 Windows 帐户授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24532361/

相关文章:

c# - 如何在 DotNetNuke 中获取门户的当前登录 URL?

c# - 调试 DocumentDB 触发器

windows - 如何从 config.txt 上的 7zip sfx 文件设置在 "RunProgram"之前或之后运行 exe?

c# - RegularExpressionAttribute 的正则表达式必须包含 "[S]"文本

c# - WinForm TabControl 选项卡页面内容在显示新选项卡之前不会更新

Windows 中的 PHP 5.5.21/Apache 2.4/MySQL 5.6.22 出现 PHP PDO "could not find driver"错误

windows - 更改 IP 和 DNS 的批处理命令

javascript - Windows 8 商店应用程序 : how to have the app live tile change constantly of colour (and text)?

javascript - Onclick html 属性在 Windows 10 UWP 应用程序中不起作用

c# - Databound Flipview remove ViewModel.ItemAt(0) 导致翻转过渡