小编And*_*Gyr的帖子

即使发送令牌,ASP.NET Identity也为null

对于我的论文项目,我必须在我的ASP.NET解决方案中实现基于令牌的(承载)身份验证.我像Taiseer Jouseh(http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity)那样实现了它.

基本部分工作正常.我有一个移动客户端,我可以在其上注册一个新用户.然后我可以登录并接收令牌.当我发出请求时,令牌将在请求标头中发送.一切正常.我的问题是,如果我调用[授权]方法,即使我发送令牌,我也会收到401 Unauthorized错误.所以我删除了[Authorize] Annotation来测试一些东西:

var z = User.Identity;
var t = Thread.CurrentPrincipal.Identity;
var y = HttpContext.Current.User.Identity;
var x = Request.GetOwinContext().Authentication.User.Identity;
Run Code Online (Sandbox Code Playgroud)

在这里我得到了相同的身份:AuthenticationType = null; IsAuthenticated = FALSE; NAME = NULL; 声明:空

var token = Request.Headers.Authorization;
Run Code Online (Sandbox Code Playgroud)

在这里,我得到了正确的令牌.因此令牌由请求发送.

我希望你能帮助我.我有令牌但没有身份.

以下是我的代码的一部分:OAuthServiceProvider:

public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{

    public async override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();
    }

    // POST /token
    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {

        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        var userManager = DependencyResolver.Current.GetService<UserManager<IdentityUser, int>>();

        IdentityUser user = await userManager.FindAsync(context.UserName, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net authentication asp.net-identity

5
推荐指数
1
解决办法
6051
查看次数

标签 统计

asp.net ×1

asp.net-identity ×1

authentication ×1

c# ×1