相关疑难解决方法(0)

使用WebApi中的OAuth Bearer Tokens Generation和Owin将更多信息返回给客户端

我创建了一个WebApi和一个Cordova应用程序.我正在使用HTTP请求在Cordova应用程序和WebAPI之间进行通信.在WebAPI中,我实现了OAuth Bearer Token Generation.

public void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider(new UserService(new Repository<User>(new RabbitApiObjectContext()), new EncryptionService()))
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

    }
Run Code Online (Sandbox Code Playgroud)

这是在SimpleAuthorizationServerProvider实现中

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

        // A little hack. context.UserName contains the email
        var user = await _userService.GetUserByEmailAndPassword(context.UserName, context.Password);

        if (user == null)
        {
            context.SetError("invalid_grant", "Wrong email or …
Run Code Online (Sandbox Code Playgroud)

c# authentication owin asp.net-web-api2 bearer-token

62
推荐指数
2
解决办法
5万
查看次数

在Asp.Net Core中动态更改连接字符串

我想在控制器中更改sql连接字符串,而不是在ApplicationDbContext中.我正在使用Asp.Net Core和Entity Framework Core.

例如:

public class MyController : Controller {
    private readonly ApplicationDbContext _dbContext
    public MyController(ApplicationDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    private void ChangeConnectionString()
    {
    // So, what should be here?
    } }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c# asp.net entity-framework-core asp.net-core

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

如何从经过身份验证的 SecurityToken 中获取声明

我将令牌作为字符串传递到 SOAP 服务中,并已验证令牌有效。我现在有一个 SecurityToken,在调试模式下我可以看到所有声明,特别是我想传递给另一个方法的 userId 声明。我似乎无法弄清楚如何获得索赔。现在我解码了令牌的字符串版本(令牌的未验证字符串版本,我至少等到验证成功之后。)这是代码块:

SecurityToken validatedToken = null;

if (VerifyToken(sPassword, ref response, ref validatedToken))
{
    var claimsObj = JObject.Parse(Encoding.UTF8.GetString(Base64Url.Decode(claims)));
    JToken userId = claimsObj.GetValue("userId");
    return implClass.Process(userId);
}
Run Code Online (Sandbox Code Playgroud)

如何从 SecurityToken 中获取声明?

validatedToken.Id; // not it
validatedToken.ToClaimsPrincipal(cert); // doesn't work
Run Code Online (Sandbox Code Playgroud)

就公开的属性而言,对我来说似乎没有其他希望,但由于我可以在调试器中看到声明,我确信有一种我没有看到的方法。

c# jwt identityserver3

6
推荐指数
2
解决办法
4697
查看次数