【问题标题】:OpenID OWIN auth and lack of user permissionsOpenID OWIN 身份验证和缺少用户权限
【发布时间】:2015-05-12 10:23:59
【问题描述】:

我可能处理的完全不正确,但我使用 OpenID 和 MS Azure 来验证我的用户,然后我检查以确保用户在 OpenID 中间件的通知中拥有用户帐户,如果未找到用户,我抛出了一个安全异常。如何返回 您无权访问此应用程序 类型的页面。我只是错过了钩子吗?

这是一个例子: https://gist.github.com/phillipsj/3200ddda158eddac74ca

【问题讨论】:

    标签: azure owin openid-connect


    【解决方案1】:

    您可以在通知中使用 try...catch,类似于以下内容:

    SecurityTokenValidated = (context) =>
    {
       try
       {
           // retriever caller data from the incoming principal
           var username = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.Name).Value.Split('@')[0];
           var database = DependencyResolver.Current.GetService(typeof (IDatabase)) as IDatabase;
           var employee = database.Query(new GetEmployeeByUsername(username));
    
           if (employee == null)
           {
              throw new SecurityTokenValidationException();
           }
             // I add my custom claims here
             context.AuthenticationTicket.Identity.AddClaims(claims);
             return Task.FromResult(0);
       }
       catch (SecurityTokenValidationException ex)
       {
           context.HandleResponse();   // This will skip executing rest of the code in the middleware
           context.Response.Redirect(....);
           return Task.FromResult(0);
       }
    }
    

    【讨论】:

    • 谢谢,我会在今天的某个时候试一试。
    猜你喜欢
    • 2019-09-11
    • 2012-12-02
    • 2017-03-06
    • 2011-06-26
    • 1970-01-01
    • 2019-07-20
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多