小编mmo*_*o79的帖子

确定承载令牌是否已过期或是否已获得授权

我的角度应用程序正在使用文章系列中所述的持有人令牌http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net - 身份/.我已经按照分叉示例在访问令牌到期时无缝刷新令牌(通过401 http代码).

我的问题是如何根据确定的角色确定持有人令牌是否已过期或仅仅未经授权?

例如,我的web api方法具有[Authorize(Roles ="Admin")]属性.当我打电话给那个时,我收到了我的401错误,这是预期的.但是,当我的访问令牌过期并且我进行另一个web api方法调用时,它也会返回401错误.继承我的拦截器中的responseError处理程序:

        responseError: function (rejection) {
            var deferred = q.defer();
            if (rejection.status === 401) {
                var authService = $injector.get('authService');
                authService.refreshToken().then(function (response) {
                    _retryHttpRequest(rejection.config, deferred);
                }, function () {
                    authService.logOut();
                    $location.path('/dashboard');
                    deferred.reject(rejection);
                });
            } else {
                deferred.reject(rejection);
            }
            return deferred.promise;
        }
Run Code Online (Sandbox Code Playgroud)

我正在玩不同的东西,但基本上,我想刷新我的令牌并在访问令牌到期时重新发送我的请求; 但是,如果由于指定的角色确实是拒绝请求,我不想刷新我的令牌.

有什么想法吗?

asp.net-web-api angularjs owin bearer-token

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

使用Windows身份验证和OWIN的ASP.NET MVC5/AngularJS/Web API应用程序

我可能过于复杂了,但是我们有一个使用Windows身份验证的AngularJS内部ASP.NET MVC5 SPA.此应用程序有一个SQL后端数据库,其中包含一个用户表,其中包含其帐户名以及它们在应用程序中各自的角色.我们将调用另一个也启用了Windows身份验证的Web API应用程序.

我曾尝试研究如何使用OWIN处理授权,但找不到任何有关OWIN和Windows身份验证的具体示例.出现的所有内容都使用带有用户名和密码的表单身份验证.

如何为我的应用程序使用OWIN和Windows Auth?这是我的OAuthAuthorizationServerProvider类的示例.

public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();
        return;
    }

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        var container = UnityHelper.GetContainerInstance("***");
        var securityHelper = container.Resolve<ISecurityHelper>();

        User currentUser = securityHelper.GetCurrentUser(); // Validates user based on HttpContext.Current.User
        if (currentUser == null)
        {
            context.SetError("invalid_grant", "The user could not be found.");
            return;
        }

        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim("sub", currentUser.AccountName));
        identity.AddClaim(new Claim("role", "user"));

        context.Validated(identity);
    } …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc windows-authentication angularjs owin

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