【问题标题】:Adding Claims in the MVC 5 app with Owin and windows authentication使用 Owin 和 Windows 身份验证在 MVC 5 应用程序中添加声明
【发布时间】:2014-09-29 13:25:46
【问题描述】:

我正在开发一个 mvc 5 Web 应用程序,其身份验证由 owin 和表单身份验证实现。

它与它提供的开箱即用的声明配合得很好。 现在我正在尝试使用 Windows 身份验证来登录系统

public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
//  app.CreatePerOwinContext(ApplicationDbContext.Create);
//  app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third         party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
});

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
//    clientId: "",
//    clientSecret: "");

//app.UseTwitterAuthentication(
//   consumerKey: "",
//   consumerSecret: "");

//app.UseFacebookAuthentication(
//   appId: "",
//   appSecret: "");

////  app.UseGoogleAuthentication(
//       clientId: "000-000.apps.googleusercontent.com",
//     clientSecret: "00000000000");
}

我已经评论了所有的提供者。

我正在尝试在登录过程中推送声明,但用户 (window.identity.principal) 已通过身份验证,我可以通过 authenticationmanger.current.user.Isauthenticated 进行检查。

我正在尝试登录,但未推送声明,但即使未触发签名命令,我也可以看到用户声明中存在声明列表。 就像 Windows 身份验证中的 owin 已经知道谁是当前用户及其名称和声明。但是我希望将一些自定义的一次性声明推送到我无法实现的现有列表中。

所有现有的声明都是类型系统声明,这让我怀疑我是否可以修改声明。

如何修改或更新或扩展现有的索赔列表?

我尝试了声明的撤销方法,它适用于表单身份验证。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 authentication owin


    【解决方案1】:
       private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {
            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    
            // Add more custom claims here if you want. 
            var claims = new Collection<Claim>
            {
                new Claim("Surname",user.ApplicantName),
                new Claim("ApplicantId",user.ApplicantId),
                new Claim("AccessCodeId",user.AccessCodeId),
                new Claim ( "Registered", "YES")
            };
    
            identity.AddClaims(claims);
    
            var principal = new ClaimsPrincipal(identity);
    
            // turn into Principal
            HttpContext.User = principal;
    
            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
        }
    

    【讨论】:

    • 那么我应该使用 ExternalCookie 还是 applicationCookie??我试试这个。谢谢你的回复
    • 我已经删除了 ExternalCookie 行,因为您没有使用第三方登录用户,所以不需要它。从代码中您已经选择了默认身份验证为 AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
    • 这应该从哪里调用? ApplicationUser 不是 .net 的一部分 - 为什么要使用第三方库?
    【解决方案2】:

    我知道这个问题已经很老了,但我最近也遇到了同样的问题,但在 Nancyfx 框架上。这也可能对其他人有所帮助。

    您只需要获取当前身份并向其中添加新声明即可。请注意,GetRolesForUser 只是一个自定义方法。您还可以添加角色以外的声明类型。

    string[] roles = GetRolesForUser(User.Identity.Name);
    
    var id = ClaimsPrincipal.Current.Identities.First();
    foreach (var role in roles)
    {
       id.Claims.Add(new Claim(ClaimTypes.Role, role));
    }
    

    我从BrockAllen找到了这个想法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 2014-01-02
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 2014-02-20
      相关资源
      最近更新 更多