我正在尝试创建一个使用 Windows Identity 和 Entity Framework Code First 的应用程序。所以我正在创建一个自定义类来保存一些数据。
public class Order {
public string Name { get; set; }
public DateTime CreatedDate { get; set; }
public IdentityUser User { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试将用户表链接到订单表(由实体框架生成)。这样我就可以执行 Order.User 来获取创建订单的用户的用户信息。
我想使用创建一个新订单
new Order {
Name = "Test",
CreatedDate = new DateTime(2014, 3, 30),
User = <What goes here?>
}
Run Code Online (Sandbox Code Playgroud)
现在如何获取当前登录用户的“IdentityUser”?或者我以错误的方式处理这个问题?
entity-framework asp.net-identity entity-framework-migrations
我有以下从 IdentityUser 派生的类。Person 类存储在数据库的 AspNetUsers 表中,并且在数据库端一切看起来都很好。
public class Person : IdentityUser
{
[Required]
[StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
public string FirstName { get; set; }
[Required]
[StringLength(150, ErrorMessage = "Last name cannot be longer than 150 characters.")]
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的问题是关于创建一个具有名字和姓氏的新用户(人)。我想,首先我需要这样做:
var user = new IdentityUser() { UserName = "testing" };
IdentityResult result = userManager.Create(user,"123456");
Run Code Online (Sandbox Code Playgroud)
这将向 AspNetUsers 表插入一个新行,其中名字和姓氏字段为空。然后,通过使用 LinQ,我需要更新现有记录的名字和姓氏字段。这种做法合理吗?还有其他推荐的方法吗?
c# linq entity-framework entity-framework-6 asp.net-identity
我有以下方法来获取存储在中的角色列表AspNetRoles
[AllowAnonymous]
public async Task<ActionResult> Register()
{
//Get the list of Roles
ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
return View();
}
Run Code Online (Sandbox Code Playgroud)
然后我看到它如下
<div class="form-group">
<label class="col-md-2 control-label">
Select User Role
</label>
<div class="col-md-10">
@foreach (var item in (SelectList)ViewBag.RoleId)
{
<input type="checkbox" name="SelectedRoles" value="@item.Value" class="checkbox-inline" />
@Html.Label(item.Value, new { @class = "control-label" })
}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但是一旦我加载页面,我就会收到Object reference not set to an instance of an object.错误
第 175 行:ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
这是RoleManager …
我试图找出 ASP.NET Identity 2.1 中两因素身份验证代码的最大值是多少。
我尝试过设置以下内容:
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(60))
Run Code Online (Sandbox Code Playgroud)
但代码的有效期并不长,所以我想知道这是否是 cookie 本身的过期时间,而不是它包含的代码的过期时间。我想知道根据验证码的生成方式,验证码的持续时间是否存在技术限制。
所有示例都只是坚持 5 分钟,所以我想知道这是否是实际的限制。我在某处读到,除了 5 分钟之外还有额外的 90 秒的时间,所以这似乎是我得到的。
我注意到这个问题(ASP.Net Identity 2, Two Factor Security Code timespan)正在寻求做同样的事情,但没有一个可接受的答案,而且它已经有将近 1.5 年的历史了,所以我想我会从一个限制开始问在我费心尝试根据该答案更改它之前,请先了解一下我的观点。
我正在尝试将Asp .Net Identity Core与Identity Server 4一起使用。我可以在日志(Id)中看到用户已正确登录。
信息:Xena.IdentityServer.Controllers.AccountController[0] 用户已登录。
然后,我的登录控制器将用户发送到我的管理控制器。
[Route("[controller]/[action]")]
[Authorize]
//[Authorize(AuthenticationSchemes = "Identity.Application")]
public class ManageController : Controller
{
[HttpGet]
public async Task<IActionResult> Index(ManageMessageId? message = null)
{
.....
}
}
Run Code Online (Sandbox Code Playgroud)
用户永远不会到达,因为登录由于某种原因丢失。
info: Microsoft.AspNetCore.Mvc.RedirectToActionResult[2]
Executing RedirectResult, redirecting to /Manage/Index.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action Xena.IdentityServer.Controllers.AccountController.Login (Xena.IdentityServer) in 3493.6102ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 3515.9158ms 302
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request starting HTTP/1.1 GET http://localhost:5000/Manage/Index
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
Authorization failed for user: (null).
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
Authorization failed for the request …Run Code Online (Sandbox Code Playgroud) c# cookies asp.net-identity asp.net-core-mvc identityserver4
我对 ASP.NET 身份服务器相当陌生。我知道如何自定义 IdentityUser 实体和脚手架/覆盖 Identity UI。鉴于这些情况,我正在寻找有关授权的高级操作方法和最佳实践:
应用程序内的组件需要考虑以下授权事项:
来自网络表单类背景,我倾向于这样思考来定义角色:
public int assignId; //key
public int userId;
public int roleId;
public int? locationId;
public int? divisionId;
public int? entityId;
Run Code Online (Sandbox Code Playgroud)
使用 asp.net core 2.1 身份处理此类授权规则/策略的最佳方法是什么?添加“loc”/“indy”等声明?
我会设置自定义授权处理程序来检查声明吗?或者我会像在传统网络表单时代那样设置一个中间件表来处理关联吗?或者这种情况有最佳实践吗?
感谢您的帮助!!!!!!!
c# claims-based-identity asp.net-identity asp.net-core asp.net-core-2.0
使用 Asp.Net Identity 允许您向用户添加声明。System.Security.Claims.ClaimTypes允许您从各种 ClaimType中选择任何 ClaimType。
ClaimTypes是一个静态类,定义了可以分配给主题的众所周知的声明类型的常量。
我想将所有这些声明存储在List<>中,并将它们显示在ListBox中,以便具有Admin角色的用户可以在注册后将 ClaimType 分配给用户。
似乎我可以做到这一点,因为ClaimTypes是一个静态类,并且其中定义的那些常量无法列出。
c# asp.net system.security claims-based-identity asp.net-identity
假设有一个身份服务器用于登录和注销。然后您就有了一个 Android 应用程序,它使用 OAuth 2.0 通过 IS4 登录和注销。现在,当您登录时,您将单击 Android 应用程序上的登录按钮,通过网络浏览器重定向到您登录的身份服务器,然后重定向回应用程序。我们还假设所有这些都有效,并且您在整个过程结束时获得了访问令牌。
我遇到的问题是注销您的行为应该是什么。现在我明白在 Android 应用程序上,您可以简单地清理访问令牌和刷新令牌,并且用户不再有权访问应用程序中的任何位置。但是,在您的浏览器上,您仍然处于登录状态。所以这是我迷失的部分。
要完全注销,我现在必须将用户重定向到 Web 浏览器以在那里注销,因此现在至少需要单击 2 次注销才能真正注销。如果我采用正确的设计,我还应该有一个确认窗口来减轻某些攻击,现在只需单击 3 次即可注销。那么在这一切之后你要去哪里呢?这是令我困扰的部分。我应该让您留在网络浏览器中还是应该将您推回应用程序登录屏幕?
只是为了澄清我的问题,因为它们实际上是两个:
我在 IdentityServer 上有一个客户端,它允许 openid、个人资料和电子邮件范围:
return new[] {
new Client
{
ClientId = "TestWebApp",
ClientSecrets = new [] { new Secret("TestSecret".Sha256()) },
AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
AllowedScopes = new List<string>{ StandardScopes.OpenId, StandardScopes.Profile,StandardScopes.Email },
}
};
Run Code Online (Sandbox Code Playgroud)
我还定义了以下身份资源,
public static IEnumerable<IdentityResource> IdentityResources()
{
return new IdentityResource[] {
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email()
};
}
Run Code Online (Sandbox Code Playgroud)
如果声明丢失,我会在创建时明确将电子邮件添加到用户声明中:
await _userManager.AddClaimAsync(testUser, new Claim("email", user.Username));
Run Code Online (Sandbox Code Playgroud)
现在从我的登录控制器使用ResourceOwnerPasswordAndClientCredentials我发送身份验证请求:
var client = new OAuth2Client(new Uri("http://localhost:44322/connect/token"), "TestWebApp", "TestSecret");
var requestResponse = client.RequestAccessTokenUserName(model.Email, model.Password, "openid profile email");
Run Code Online (Sandbox Code Playgroud)
这工作正常,我正在取回范围,但它们都是空白的。
感谢新发布的模块;AWS Cognito 现在是 ASP.NET Core 身份提供商。
通过以下方式进行基于角色的授权会很好[Authorize(Roles= "{Role}")];但目前它不起作用;部分原因可能是因为认知(群体/角色)被映射到cognito:groups声明。
有办法让它发挥作用吗?(除了编写自定义策略之外)
还提交了一个问题:https://github.com/aws/aws-aspnet-cognito-identity-provider/issues/86
其他参考:
asp.net-identity ×10
c# ×6
asp.net ×2
asp.net-core ×2
asp.net-mvc ×1
cookies ×1
entity-framework-migrations ×1
identity ×1
linq ×1
logout ×1
oauth ×1
oauth-2.0 ×1
role-manager ×1