簡體   English   中英

getUserId在rc1.final的User.Identity中不存在

[英]getUserId does not exist in User.Identity in rc1.final

我正在使用asp.net 5和實體框架7創建Web應用程序,並且在使User.Identity正常工作時遇到一些問題。 文檔中 ,它說它位於名稱空間User.Identity.Core 當我通過nuget安裝它時,它會找到方法。 但是,我與UserManager發生沖突,因為它說它同時存在於User.Identity.CoreUser.Identity.EntityFramework 如果我卸載User.Identity.EntityFramework ,則不允許在我的用戶模型中從IdentityUser繼承。 我需要GetUserId才能獲取當前登錄的旅館用戶...我也嘗試使用

System.Web.HttpContext.Current.User.Identity.GetUserId(); 

但是似乎有一個類似的問題,它說CurrentHttpContext中不存在。

有人知道解決方案嗎?

使用project.json更新:

{
  "userSecretsId": "aspnet5-FrkKantine-1a8100b9-6fce-44b4-ac9d-6038ecf705f6",
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Razor": "4.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": { },
    "dnx50": {}
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
  }
}

由於ASP.NET Core 1.0(以前的ASP.NET 5)仍在開發中,因此情況經常發生變化。

請查看GitHub 公告追蹤器,了解API對ASP.NET Core 1.0版本的更改(不過不要在此處發布問題,這純粹是由ASP.NET Core團隊發布公告)。

這些公告之一是關於GetUsernameGetUserIdIsSignedIn擴展方法的移動。

這些擴展方法所引用的聲明可以通過IdentityOptions進行配置,而該擴展方法當前始終不被ClaimssTypes.NameIdentifier / Name中引用構建的擴展方法所遵循。

這些方法已被解決:

 User.GetUserId => UserManager.GetUserId(User) User.GetUserName => UserManager.GetUserName(User) User.IsSignedIn => SignInManager.IsSignedIn(User) 

編輯:我剛剛注意到它是針對RC2的,因此除非您使用每夜構建供稿,否則可能還不適用於您。

您的問題可能是由於選擇了錯誤的軟件包名稱引起的,因為它們在過去曾被重命名過六次。

EntityFramework.MicrosoftSqlServerMicrosoft.AspNet.Identity.EntityFramework應該適合您。 (使用RC2,他們又重新命名為Microsoft.EntityFrameworkCore.*Microsoft.AspNetCore.* ,所有版本都重置為1.0)

解決方案1:

嘗試這個 :

using Microsoft.AspNet.Identity;

接着 :

string userId = User.Identity.GetUserId();

解決方案2:

通常,您應該確保用戶是否通過身份驗證,然后獲取用戶信息/ ID:

if (model != null)
    {
        model.IsUserAuthenticated = HttpContext.User.Identity.IsAuthenticated;

        if (model.IsUserAuthenticated)
        {
            model.UserName = HttpContext.User.Identity.Name;
        }
    }

如果您使用的是.Net Core <= RC1 Update 1,則仍可以使用GetUserId()擴展名。

所需參考資料:

using Microsoft.AspNet.Identity;
using System.Security.Claims;

用法: var id = User.GetUserId(); (而不是User.Identity.GetUserId()

您提到的另一件事是HttpContext.Current為null,我假設您正在嘗試從類庫或MVC范圍之外訪問用戶。

如果需要從類庫訪問HttpContext,則不能像靜態屬性那樣僅訪問它。 需要在該類中注入

只需執行以下操作:

private readonly IHttpContextAccessor _httpContextAccessor;

public MyRepository(IHttpContextAccessor httpContextAccessor)
{
    _httpContextAccessor = httpContextAccessor;
}

然后,您可以通過此類中的所有方法使用它:

_httpContextAccessor.HttpContext.User.GetUserId();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM