在使用MVC5之前我已经完成了这个,User.Identity.GetUserId()但这似乎不适用于此.本User.Identity可是没有的GetUserId()方法
我在用 Microsoft.AspNet.Identity
使用Owin cookie身份验证时遇到一个奇怪的问题.
当我启动我的IIS服务器时,身份验证在IE/Firefox和Chrome上运行得非常好.
我开始使用身份验证进行一些测试并在不同的平台上登录,我想出了一个奇怪的错误.偶尔Owin框架/ IIS只是不向浏览器发送任何cookie.我将输入一个用户名和密码,该代码运行正确,但根本没有cookie传递给浏览器.如果我重新启动服务器它开始工作,那么在某些时候我将尝试登录并再次cookie停止交付.单步执行代码不会做任何事情并且不会抛出任何错误.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
CookieHttpOnly = true,
AuthenticationType = "ABC",
LoginPath = new PathString("/Account/Login"),
CookiePath = "/",
CookieName = "ABC",
Provider = new CookieAuthenticationProvider
{
OnApplyRedirect = ctx =>
{
if (!IsAjaxRequest(ctx.Request))
{
ctx.Response.Redirect(ctx.RedirectUri);
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
在我的登录程序中,我有以下代码:
IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var authentication = HttpContext.Current.GetOwinContext().Authentication;
var identity = new ClaimsIdentity("ABC");
identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.User_ID.ToString()));
identity.AddClaim(new Claim(ClaimTypes.Role, role.myRole.ToString()));
authentication.AuthenticationResponseGrant =
new AuthenticationResponseGrant(identity, new AuthenticationProperties()
{
IsPersistent = isPersistent
}); …Run Code Online (Sandbox Code Playgroud) 我正在使用MVC5 Identity 2.0登录我的网站,其中身份验证详细信息存储在SQL数据库中.Asp.net Identity已经以标准方式实现,可以在许多在线教程中找到.
IdentityModels中的ApplicationUser类已扩展为包含一些自定义属性,例如整数OrganizationId.我们的想法是,可以创建许多用户并将其分配给公共组织以实现数据库关系.
public class ApplicationUser : IdentityUser
{
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;
}
//Extended Properties
public DateTime? BirthDate { get; set; }
public long? OrganizationId { get; set; }
//Key Mappings
[ForeignKey("OrganizationId")]
public virtual Organization Organization { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何从控制器中检索当前登录用户的OrganizationId属性?一旦用户登录,这是否可通过方法获得,或者每次执行控制器方法时,我是否始终根据UserId从数据库中检索OrganizationId?
在网上阅读我看到我需要使用以下内容来登录UserId等.
using Microsoft.AspNet.Identity;
...
User.Identity.GetUserId();
Run Code Online (Sandbox Code Playgroud)
但是,OrganizationId不是User.Identity中可用的属性.我是否需要扩展User.Identity以包含OrganizationId属性?如果是这样,我该怎么做呢.
我经常需要OrganizationId的原因是许多表查询依赖于OrganizationId来检索与登录用户相关联的与组织相关的数据.
asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
我正在创建一个新的应用程序并开始使用EF6-rc1,Microsoft.AspNet.Identity.Core 1.0.0-rc1,Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1,Microsoft.AspNet.Identity .Owin 1.0.0-rc1等昨天和RTM发布时,我今天晚上通过NuGet更新了它们到RTM.
除了我到目前为止所完成的工作的一些代码更改,所有似乎都进展顺利,直到我尝试为应用程序创建本地用户帐户.
我一直在处理电子邮件地址作为用户名格式,发布候选人工作得很好,但现在在创建一个用户名的电子邮件地址的用户时,它会引发以下验证错误:
User name xxxxx@xxxx.com is invalid, can only contain letters or digits.
Run Code Online (Sandbox Code Playgroud)
我花了大约一个小时左右的时间来搜索有关配置选项的解决方案或文档,但无济于事.
有没有办法可以配置它以允许用户名的电子邮件地址?
有什么区别:
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
IdentityResult result = IdentityManager.Authentication.CheckPasswordAndSignIn(AuthenticationManager, model.UserName, model.Password, model.RememberMe);
if (result.Success)
{
return Redirect("~/home");
}
else
{
AddErrors(result);
}
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
和:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
IdentityResult result = await IdentityManager.Authentication.CheckPasswordAndSignInAsync(AuthenticationManager, model.UserName, model.Password, model.RememberMe);
if (result.Success)
{
return Redirect("~/home");
}
else
{
AddErrors(result);
}
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
我看到MVC代码现在有异步,但有什么区别.一个人的表现比另一个人好得多吗?调试一个问题比另一个问题更容易吗?我是否应该为我的应用程序更改其他控制器以添加异步?
asp.net-mvc task-parallel-library async-await asp.net-mvc-5 asp.net-identity
我想让当前用户获取用户的信息,例如电子邮件.但我不能在asp.net核心中这样做.我很困惑这是我的代码.
HttpContext在控制器的构造函数中几乎为null .在每个动作中获得用户并不好.我想获得用户的信息并将其设置为ViewData;
public DashboardController()
{
var user = HttpContext.User.GetUserId();
}
Run Code Online (Sandbox Code Playgroud) 我们如何在安全的ApiController操作中获取当前用户,而不将userName或userId作为参数传递?
我们假设这是可用的,因为我们处于安全行动中.处于安全行动意味着用户已经过身份验证,并且请求具有她的承载令牌.鉴于WebApi已授权用户,可能存在一种内置方式来访问userId,而不必将其作为操作参数传递.
如何在新的ASP.NET身份系统中获取用户的密码?或者如何在不知道当前密码(用户忘记密码)的情况下重置?
是否可以将新的Asp.net标识与Database First和EDMX一起使用?或者只使用代码?
这是我做的:
1)我创建了一个新的MVC5项目并让新的Identity在我的数据库中创建了新的User和Roles表.
2)然后我打开了我的Database First EDMX文件并拖入了新的Identity Users表,因为我有其他与之相关的表.
3)保存EDMX后,Database First POCO生成器将自动创建User类.但是,UserManager和RoleManager需要从新的Identity命名空间(Microsoft.AspNet.Identity.IUser)继承User类,因此使用POCO User类将不起作用.
我想一个可能的解决方案是编辑我的POCO生成类以使我的User类继承自IUser?
或者ASP.NET Identity仅与Code First Design兼容?
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
更新:根据Anders Abel的建议,这就是我所做的.它的工作原理,但我想知道是否有更优雅的解决方案.
1)我通过在与自动生成的实体相同的命名空间中创建一个部分类来扩展我的实体User类.
namespace MVC5.DBFirst.Entity
{
public partial class AspNetUser : IdentityUser
{
}
}
Run Code Online (Sandbox Code Playgroud)
2)我将DataContext更改为继承IdentityDBContext而不是DBContext.请注意,每次更新EDMX并重新生成DBContext和Entity类时,都必须将其设置为此.
public partial class MVC5Test_DBEntities : IdentityDbContext<AspNetUser> //DbContext
Run Code Online (Sandbox Code Playgroud)
3)在自动生成的User实体类中,必须将override关键字添加到以下4个字段中,或者将这些字段注释掉,因为它们是从IdentityUser继承的(步骤1).请注意,每次更新EDMX并重新生成DBContext和Entity类时,都必须将其设置为此.
override public string Id { get; set; }
override public string UserName { get; set; }
override public string PasswordHash { get; set; }
override public string SecurityStamp { get; set; }
Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc ef-database-first asp.net-mvc-5 asp.net-identity
关于使用新的Asp.net身份安全框架的文档很少.
我已经拼凑了我可以尝试创建一个新角色并添加一个用户.我尝试了以下内容:在ASP.NET身份中添加角色
看起来它可能从这个博客获得了信息:使用asp.net身份构建一个简单的待办事项应用程序并将用户与to-do相关联
我已将代码添加到数据库初始化程序,该数据库初始化程序在模型更改时运行.它在RoleExists函数上失败并出现以下错误:
System.InvalidOperationException发生在mscorlib.dll中实体类型IdentityRole不是当前上下文的模型的一部分.
protected override void Seed (MyContext context)
{
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
// Create Admin Role
string roleName = "Admins";
IdentityResult roleResult;
// Check to see if Role Exists, if not create it
if (!RoleManager.RoleExists(roleName))
{
roleResult = RoleManager.Create(new IdentityRole(roleName));
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
asp.net-identity ×10
asp.net ×6
asp.net-mvc ×3
c# ×3
asp.net-core ×1
async-await ×1
katana ×1
owin ×1