简体   繁体   English

在外部ADFS登录上加载任何页面之前获取用户声明

[英]Get user claims before any page loads on external ADFS login

What I'm trying to do is accessing user claims which returns from ADFS login. 我正在尝试访问从ADFS登录返回的用户声明。 ADFS returns username and with that username I have to run a query to another DB to get user information and store it. ADFS返回用户名,使用该用户名我必须对另一个数据库运行查询以获取用户信息并将其存储。 I don't really know where to do that and what the best practice is. 我真的不知道该在哪里做以及最佳实践是什么。 I can access user claims in the view controller like: 我可以在视图控制器中访问用户声明,例如:

public ActionResult Index()
{
    var ctx = Request.GetOwinContext();
    ClaimsPrincipal user = ctx.Authentication.User;
    IEnumerable<Claim> claims = user.Claims;
    return View();
}

But what I need to do is as I said access claims like in global.asax.cs or startup.cs to store user information before the application runs. 但是我需要做的就是如我所说的访问声明,例如在global.asax.cs或startup.cs中,以便在应用程序运行之前存储用户信息。

This is my Startup.Auth.cs file: 这是我的Startup.Auth.cs文件:

public partial class Startup
{
    private static string realm = ConfigurationManager.AppSettings["ida:Wtrealm"];
    private static string adfsMetadata = ConfigurationManager.AppSettings["ida:ADFSMetadata"];

    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(
            new CookieAuthenticationOptions
            {
                AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
            });

        app.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                Wtrealm = realm,
                MetadataAddress = adfsMetadata
            });
    }
}

We add an event handler to the WsFederationAuthenticationOptions value in our startup file. 我们将事件处理程序添加到启动文件中的WsFederationAuthenticationOptions值中。

This happens immediately after the security token has been validated. 验证安全令牌后立即发生这种情况。

app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions()
{
    MetadataAddress = MetadataAddress,

    Wtrealm = Wtrealm,
    Wreply = CallbackPath,
    Notifications = new WsFederationAuthenticationNotifications()
    {
        SecurityTokenValidated = (ctx) =>
        {
           ClaimsIdentity identity = ctx.AuthenticationTicket.Identity;
           DoSomethingWithLoggedInUser(identity);
        }
     }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM