目前有ApplicationUser类具有一些自定义属性,如:
public class ApplicationUser : IdentityUser
{
public string Name { get; set; }
public List<Content> Content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想让当前登录的用户获得相关数据列表(Content属性).
在我的控制器中,如果我把:
Applicationuser user = await _userManager.GetUserAsync(HttpContext.User);
Run Code Online (Sandbox Code Playgroud)
我得到了登录用户,但没有任何相关数据.但是,如果我使用ApplicationDbContext下面的检索当前用户,我可以检索相关数据:
ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);
ApplicationUser userWithContent = _context.Users.Include(c => c.Content).Where(u => u.Id == user.Id).ToList();
Run Code Online (Sandbox Code Playgroud)
但这对我来说似乎不正确!
任何的想法?
我试图覆盖当前请求的文化.我使用自定义部分工作ActionFilterAttribute.
public sealed class LanguageActionFilter : ActionFilterAttribute
{
private readonly ILogger logger;
private readonly IOptions<RequestLocalizationOptions> localizationOptions;
public LanguageActionFilter(ILoggerFactory loggerFactory, IOptions<RequestLocalizationOptions> options)
{
if (loggerFactory == null)
throw new ArgumentNullException(nameof(loggerFactory));
if (options == null)
throw new ArgumentNullException(nameof(options));
logger = loggerFactory.CreateLogger(nameof(LanguageActionFilter));
localizationOptions = options;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
string culture = context.RouteData.Values["culture"]?.ToString();
if (!string.IsNullOrWhiteSpace(culture))
{
logger.LogInformation($"Setting the culture from the URL: {culture}");
#if DNX46
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
#else
CultureInfo.CurrentCulture = new CultureInfo(culture); …Run Code Online (Sandbox Code Playgroud) 我正在研究MVC 6应用程序(DNX Core 5.0框架).不幸的是,我找不到任何pdf导出库.
任何帮助将不胜感激.
我有一个使用ASP.NET Core RC2在MVC6中构建的Intranet站点.我想获取访问Intranet站点的人的Windows用户名.
因此,如果Jim访问内部网站点,我希望该站点接收"Domain\Jim",而如果Anne访问内部网站,我希望该站点接收"Domain\Anne".
我的IIS服务器上仅启用了Windows身份验证,禁用了匿名身份验证.
我的网站设置为使用Windows身份验证并禁用匿名身份验证.
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)
通过调试我可以使用System.Security.Principal.WindowsIdentity.GetCurrent().Name,但当然在IIS服务器上返回"IIS APPPOOL\SiteName".
我使用HttpContext从旧版本的ASP.NET中找到了许多示例,我尝试使用以下内容将其注入到我的控制器中,但userName最终为null.
//Startup.cs
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//HomeController.cs
public HomeController(IHttpContextAccessor _accessor)
{
string userName = _accessor.HttpContext.User.Identity.Name;
}
Run Code Online (Sandbox Code Playgroud)
将Windows用户名传递给ASP.NET Core RC2 MVC6中的Intranet站点的正确方法是什么?
在旧版本的MVC 5中,我可以这样做来传递路由参数
@Html.ActionLink("New item", "Suggestion", new ProductSuggestion() { Username = Model.Username }, new { @class = "btn btn-default" })
Run Code Online (Sandbox Code Playgroud)
我试图让这个与新的asp-action方法一起工作,我发现我可以做这个解决方法.但是这个的正确语法是什么?
@{
var a = new Dictionary<string, string> { { "Username", Model.Username } };
}
<a asp-action="Suggestion" asp-all-route-data="a" class="btn btn-default">New item</a>
Run Code Online (Sandbox Code Playgroud) 我正在使用.NET Core开发ASP.NET Web API.此Web API将主要由UI应用程序访问(UI将使用ASP.NET Core MVC开发),但在将来API也可以由其他应用程序访问.
在我的WEB API中,所有方法都是异步的.
如果我希望客户端进行内容协商,那么应该是API操作方法的返回类型Task<IActionresult>或Task<SomePOCO>
如果我希望方法总是以JSON格式返回数据,那么API动作方法的返回类型应该是什么?它应该是Task<IActionResult>或Task<JsonResult>还是Task<SomePOCO>因为我认为所有3将工作,所以不知道哪一个是合适的吗?
来源有评论说TryAdd如果版本已经注册,则版本不会添加服务IServiceCollection.但是docs没有提到这种方法.应该什么时候使用?
在ASP.NET Core Web应用程序的控制器中,我想刷新存储在客户端上的cookie票证中的用户和声明.
客户端经过身份验证和授权,ASP.NET Core Identity将此信息存储在cookie票证中 - 现在在某些Controller操作中我想刷新cookie中的数据.
该SignInManager有一个函数来刷新RefreshSignInAsync,但它不接受HttpContext.User作为参数.
[HttpPost("[action]")]
[Authorize]
public async Task<IActionResult> Validate()
{
// todo: update the Client Cookie
await _signInManager.RefreshSignInAsync(User); // wrong type
}
Run Code Online (Sandbox Code Playgroud)
如何刷新cookie?
ServiceFilter我们必须在Startup.cs中注册.TypeFilter是由Microsoft.Extensions.DependencyInjection.ObjectFactory注入的,我们不需要注册那个过滤器.
那么当我们应该使用ServiceFilter和TypeFilter时?
Visual Studio 2015脚手架使用UserManager<TUser>不能用于创建的脚手架ClaimsIdentity.有没有人有一个如何做到这一点的工作示例?
VS2015脚手架抛出错误:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one
// defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
Run Code Online (Sandbox Code Playgroud)
注意:我添加了ApplicationUser与之不冲突的属性IdentyUser.