![](/img/trans.png)
[英]Can I use SharePoint to authenticate users on a separately hosted website?
[英]how can I use a Microsoft Account to authenticate to my website
我有一個需要用戶身份的網站,我真的不想讓他們創建另一個他們必須記住的用戶名/密碼組合
是否有SDK允許從Microsoft帳戶進行身份驗證?
這很簡單,因為ASP.NET 4.5網站的默認空模板顯示了如何使用google / facebook / liveid / twitter進行OAuth2身份驗證。
http://www.asp.net/aspnet/overview/aspnet-45/oauth-in-the-default-aspnet-45-templates
查看Principal Context類。 您可以使用localhost(計算機)或域上下文創建它,並使用ValidateCrentials(字符串用戶名,字符串密碼)方法使用Windows憑據進行身份驗證。
http://msdn.microsoft.com/en-us/library/bb154889.aspx
這是我在我的網站上使用它的方式。 (把它放在你的身份驗證控制器的POST方法或其他東西)
下面的代碼將使用用戶名“bob”或“localhost \\ bob”或“DOMAIN \\ bob”等,並獲取正確的PrincipalContext用於驗證用戶。 注意:這里不區分大小寫。
public bool ValidateCredentials(string username, System.Security.SecureString password)
{
string domain = Environment.MachineName;
if (username.Contains("\\"))
{
domain = username.Split('\\')[0];
username = username.Split('\\')[1];
}
if (domain.Equals("localhost", StringComparison.CurrentCultureIgnoreCase))
domain = Environment.MachineName;
if (domain.Equals(Environment.MachineName, StringComparison.CurrentCultureIgnoreCase))
using (PrincipalContext context = new PrincipalContext(ContextType.Machine))
{
return context.ValidateCredentials(username, password.ToUnsecureString());
}
else
using(PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
//return context.ValidateCredentials(domain + "\\" + username, password.ToUnsecureString());
return context.ValidateCredentials(username, password.ToUnsecureString());
}
}
Microsoft提供Live Connect SDK,用於將Microsoft服務集成到您的應用程序中,包括Microsoft帳戶身份提供程序。
服務器端方案中有一個特定示例,它應該涵蓋集成所需的全部內容。
你的意思是從活動目錄Windows帳戶? 如果是這樣,您可以使用Windows身份驗證,只需讓索引頁面自動簽名。
http://msdn.microsoft.com/en-us/library/ff647405.aspx
在代碼隱藏文件中使用以下命令以獲取有關登錄的相關信息:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
User.Identity.IsAuthenticated
User.Identity.AuthenticationType
User.Identity.Name
微軟的變更/品牌重塑/棄用/死鏈接給我帶來了瘋狂。 在任何情況下,我發現的最新版本是“Microsoft帳戶外部登錄”,可以首先在Microsoft Developer Portal上設置 。
我在https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/microsoft-logins上找到了一個解釋如何為.Net Core做這個的指南,雖然是上半部分(例如設置)重定向URI)不是特定於框架的。
我還在https://github.com/aspnet/Security/blob/master/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountOptions.cs上找到了.Net Core的相關源代碼,其中顯示了一些聲明(用戶)檢索的詳細信息):
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
ClaimActions.MapJsonKey(ClaimTypes.Name, "displayName");
ClaimActions.MapJsonKey(ClaimTypes.GivenName, "givenName");
ClaimActions.MapJsonKey(ClaimTypes.Surname, "surname");
ClaimActions.MapCustomJson(ClaimTypes.Email,
user => user.Value<string>("mail") ?? user.Value<string>("userPrincipalName"));
最新版本的.Net Core的支持告訴我,這個外部登錄API仍然有效。 我還沒有測試它們,如果我要進行這種登錄集成,我會更新。
答案已過期 - 微軟已更改鏈接
終於找到了類似於Facebook / Google身份驗證的Javascript庫
https://msdn.microsoft.com/en-au/library/ff748792.aspx
和
http://isdk.dev.live.com/dev/isdk/ISDK.aspx?category=scenarioGroup_core_concepts&index=0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.