我試圖在使用Facebook連接的ASP.NET MVC應用程序中對用戶進行身份驗證。 我寫了使用它獲取的Facebook的access_token,Facebook的用戶的電子郵件,然後對用戶進行認證控制器:認證後User.Identity.Name爲空
FormsAuthentication.SetAuthCookie(loggedUserEmail, true);
這一切後我將用戶重定向到主網頁,我嘗試獲取User.Identity.Name顯示登錄用戶的名稱(在這種情況下,我猜可能是電子郵件)。 不幸的是,User.Identity.Name爲null。 然而,認證過程似乎工作作爲Request.IsAuthenticated返回true ...
@if (Request.IsAuthenticated) {
<p>
@* ###THIS LINE THROWS EXCEPTION AS User.Identity.Names IS NULL### Hello, @Html.ActionLink(User.Identity.Name, "ChangePassword", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Change password" })!*@
@Html.ActionLink("Log off", "LogOff", "Account")
</p>
}
爲什麼User.Identity.Name不會啓動任何想法? 謝謝,
巴特
附: 而不是
FormsAuthentication.SetAuthCookie(loggedUserEmail, true);
我也試過
var authCookie = FormsAuthentication.GetAuthCookie(loggedUserEmail, true);
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
// We want to change the expiration of our forms authentication cookie
// to match the token expiration date, but you can also use your own expiration
DateTime expiration = ticket.IssueDate.AddSeconds(facebookTokenExpirationSeconds);
var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name,
ticket.IssueDate, expiration,
ticket.IsPersistent, facebookName);
// Encrypt the cookie again
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
// Manually set it (instead of calling FormsAuthentication.SetAuthCookie)
Response.Cookies.Add(authCookie);
,但它也不會使User.Identity.Names
包含什麼...
當你通過你的代碼是什麼,你傳遞給'FormsAuthentication.SetAuthCookie'方法'loggedUserEmail'變量的值階梯?注意,如果第一個參數爲空,'Html.ActionLink'將引發異常。 –
看起來像我犯了相當愚蠢的錯誤,問題確實在loggedUserEmail內容...:/對不起,以前不仔細檢查,會節省大量的時間。感謝提及它! – Bart
你可以學習如何格式化代碼,所以我們不必? http://stackoverflow.com/editing-help#code - 謝謝。 – Kev