无法在身份server3中获取令牌

时间:2018-01-02 08:43:05

标签: c# asp.net-mvc identity identityserver3

我是身份服务器3的新手。

我可以在其他项目中访问我的令牌,但我无法在我的身份项目中获取它。

这是我的客户端类

new Client
{
    Enabled = true,
    ClientName = "Web Application1",
    ClientId = "WebApplication1",
    Flow = Flows.Implicit,
    RequireConsent = false,
    AllowRememberConsent = true,
    RedirectUris = new List<string>
    {
        "http://localhost:52249/"
    },
    PostLogoutRedirectUris = new List<string>
    {
        "http://localhost:52249/"
    },
    IdentityTokenLifetime = 360,
    AccessTokenLifetime = 3600,
    AllowedScopes = new List<string>() { "openid", "profile" , "roles", "WebAPI" }
},
new Client
{
    Enabled = true,
    ClientName = "Identity 2",
    ClientId = "Identity",
    Flow = Flows.Implicit,
    RequireConsent = false,
    AllowRememberConsent = true,
    RedirectUris = new List<string>
    {
        "https://localhost:44396/"
    },
    PostLogoutRedirectUris = new List<string>
    {
        "https://localhost:44396/"
    },
    IdentityTokenLifetime = 360,
    AccessTokenLifetime = 3600,
    AllowedScopes = new List<string>() { "openid", "profile" , "roles", "WebAPI" }
},

和我的范围类:

var scopes = new List<Scope>
{
    new Scope
    {
        Enabled = true,
        Name = "roles",
        Type = ScopeType.Identity,
        Claims = new List<ScopeClaim>
        {
            new ScopeClaim("role")
        }
    },
    new Scope
    {
        Enabled = true,
        DisplayName = "WebAPI",
        Name = "WebAPI",
        Description = "Secure WebAPI",
        Type = ScopeType.Resource,
        Claims = new List<ScopeClaim>
        {
            new ScopeClaim(Constants.ClaimTypes.Name),
            new ScopeClaim(Constants.ClaimTypes.Role),
        }
    }};

    scopes.AddRange(StandardScopes.All);
    return scopes;
}}

和用户类:

IdentityContext db = new IdentityContext();
var AllUsers = db.Users.ToList();
List<InMemoryUser> UsersList = new List<InMemoryUser>();
foreach (var item in AllUsers)
{
    InMemoryUser UserInMemory = new InMemoryUser();
    UserInMemory.Username = item.UserName;
    UserInMemory.Password = item.Password;
    UserInMemory.Subject = item.Id.ToString();
    UserInMemory.Claims = new Claim[]
    {
        new Claim(Constants.ClaimTypes.PreferredUserName, item.UserName),
    };
}
return UsersList;

和我的身份项目中的启动课:

public void Configuration(IAppBuilder app)
{
    app.Map("/identity", idsrvApp =>
    {
        var factory =
            new IdentityServerServiceFactory().UseInMemoryClients(Clients.Get())
                                              .UseInMemoryScopes(Scopes.Get())
                                              .UseInMemoryUsers(Users.Get());

        var userService = new UserService();

        factory.UserService = new Registration<IUserService>(reslove => userService);
        var viewOptions = new DefaultViewServiceOptions();
        //viewOptions.Stylesheets.Add("/Content/Site.css");
        viewOptions.Stylesheets.Add("/Content/bootstrap-rtl.css");
        viewOptions.CacheViews = false;
        factory.ConfigureDefaultViewService(viewOptions);
        var options = new IdentityServerOptions
        {
            SiteName = "IdentityServer3 - Configuring DefaultViewService",

            SigningCertificate = LoadCertificate(),
            Factory = factory,

            AuthenticationOptions = new IdentityServer3.Core.Configuration.AuthenticationOptions
            {
                //   IdentityProviders = ConfigureAdditionalIdentityProviders,
            }
        };

        idsrvApp.UseIdentityServer(options);
    });

    Serilog.Log.Logger =
        new LoggerConfiguration().MinimumLevel.Debug()
            .WriteTo.RollingFile(pathFormat: @"c:\logs\IdSvrAdmin-{Date}.log")
            .CreateLogger();
    }

    X509Certificate2 LoadCertificate()
    {
        return new X509Certificate2(
            $"{AppDomain.CurrentDomain.BaseDirectory}\\bin\\idsrv3test.pfx", "idsrv3test");
    }

请帮帮我。 谢谢!

1 个答案:

答案 0 :(得分:1)

我认为这很有帮助,你可以想出解决这个问题。

public HttpRequestWrapper TokenizeRequest(User user, string clientid)
{
    var token = GetToken(user, clientid).Result;
    _request.AddHeader("Authorization", $"Bearer {token.AccessToken}");
    return this;
}

private async Task<TokenResponse> GetToken(User user, string clientid)
{
    var client = new TokenClient(Constants.TokenEndpoint, clientid, SecretApi);

    return  client.RequestResourceOwnerPasswordAsync
           (user.UserName, user.Password, "WebAPI").Result;
}

参考: https://www.codeproject.com/Articles/1163131/IdentityServer-WebAPI-MVC-ASP-NET-Identity-Specflo