相关疑难解决方法(0)

如何扩展User.Identity的可用属性

我正在使用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

125
推荐指数
4
解决办法
10万
查看次数

服务器端声称使用Owin身份验证进行缓存

我有一个过去常用的应用程序,FormsAuthentication不久之前我将其切换为使用IdentityModelfrom,WindowsIdentityFramework以便我可以从基于声明的身份验证中受益,但使用和实现它相当难看.所以现在我在看OwinAuthentication.

我在看OwinAuthenticationAsp.Net Identity框架.但是Asp.Net Identity框架目前唯一的实现是使用EntityModel和我正在使用nHibernate.所以现在我想尝试绕过Asp.Net Identity并直接使用Owin Authentication.我终于能够使用" 我如何忽略身份框架魔法并使用OWIN auth中间件来获取我所寻求的声明? "中的提示进行工作登录,但现在我持有声明的cookie相当大.当我使用时,IdentityModel我能够使用服务器端缓存机制来缓存服务器上的声明,而cookie只是为缓存的信息保存了一个简单的令牌.是否有相似的功能OwinAuthentication,或者我必须自己实现它?

我希望我会参加其中一艘船......

  1. cookie保持3KB,哦,它有点大.
  2. 启用类似于IdentityModelSessionCaching的功能Owin,我不知道.
  3. 编写我自己的实现来缓存导致cookie膨胀的信息,看看我是否可以Owin在应用程序启动时配置它.
  4. 我这样做是错的,有一种我没有想过的方法,或者我在滥用某些东西Owin.

    public class OwinConfiguration
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "Application",
                AuthenticationMode = AuthenticationMode.Active,
                CookieHttpOnly = true,
                CookieName = "Application",
                ExpireTimeSpan = TimeSpan.FromMinutes(30),
                LoginPath = "/Login",
                LogoutPath = …
    Run Code Online (Sandbox Code Playgroud)

asp.net authentication claims-based-identity owin

32
推荐指数
2
解决办法
1万
查看次数

如何在aspnet身份中进行会话管理?

我使用Asp.net的身份进行登录,注册,忘记密码等和源代码从该采取以下链接:

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset

http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity.

现在我有一个UserMaster表,在注册期间我要求以下字段: FullName,EmailId,Password,ContactNumber,Gender.

我的UserMaster包含以下字段:Id,FullName,EmailId,ContactNumber,Gender

现在,当用户提交注册表格时,此FullName,EmailId,ContactNumber,Gender将与电子邮件一起保存在UserMaster中,密码将保存在AspnetUser中.

我的注册方法与上面2个链接中提供的相同.

在这里您可能会注意到我的UserMaster和AspnetUser之间没有任何关系,所以在登录时用户将输入他的电子邮件ID登录我将使用此方法await SignInManager.PasswordSignInAsync验证用户,如果此方法返回成功,那么我将要使用此电子邮件ID并在我的UserMaster中检查此电子邮件,并找到匹配的地方我将从UserMaster获取UserId并将其存储在会话中并使用我的登录方法使用我的应用程序,如下所示:

 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
            {
                if (!ModelState.IsValid)
                {
                    return View(model);
                }

                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, change to shouldLockout: true
                var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
                switch (result)
                {
                    case SignInStatus.Success:
                  using (var context …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc owin asp.net-identity

15
推荐指数
1
解决办法
1万
查看次数

如何在ASP.Net MVC 5视图中获取ApplicationUser的自定义属性值?

ASP.Net MVC 5,ApplicationUser可以扩展为具有自定义属性.我已经扩展它,现在它有一个新属性叫做DisplayName:

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser {
    public string ConfirmationToken { get; set; }
    public string DisplayName { get; set; } //here it is!

    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 …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity

8
推荐指数
1
解决办法
3222
查看次数