小编Thi*_*Guy的帖子

在自定义AuthorizeAttribute中访问QueryString

我正在使用Web API并设置了一个简单的身份验证和授权机制,其中调用者传递我在查询字符串中发给他们的令牌.所以他们提交的请求如下:

https://mysite.com/api/Ping?token=[issued-token]
Run Code Online (Sandbox Code Playgroud)

我有一个像这样的ApiAuthorizeAttribute:

public class ApiAuthorizeAttribute : System.Web.Http.AuthorizeAttribute
{
    public ApiPermission Permission { get; set; }

    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        switch (Permission)
        {
            case ApiPermission.None: 
               return;

           case ApiPermission.Write:
           case ApiPermission.Read:

               string query = actionContext.Request.RequestUri.Query;
               var nvc = System.Web.HttpUtility.ParseQueryString(query);
               string token = nvc["token"];

               // (my code to map the token to an Authorization for the request)               
               ApiAuthorization auth = ApiToken.GetAuthorization(token);
               if (auth != null && auth.HasPermission(Permission))
                   return;

               HandleUnauthorizedRequest(actionContext);
               return;

           default:
               throw new ArgumentException("Unexpected Permission");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我可以像这样装饰我的API.注意:这只是一个示例,真正的呼叫将从其帐户读取数据(帐户标识符在其令牌中加密)并返回它. …

c# asp.net-mvc asp.net-web-api

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

如何使用Twitter OAuth指定范围?

使用Twitter OAuth,用户可以获得以下信息:

Twitter应用权限请求

我只需要发布推文,是否可以只询问?

Facebook在请求身份验证时支持"范围"参数,但我没有看到类似Twitter的内容: Twitter授权

twitter-oauth

7
推荐指数
1
解决办法
3479
查看次数

MVC4应用程序"无法加载DLL'libmp3lame.32.dll'

我试图在MVC4应用程序中使用NAudio.Lame库并收到错误:

Unable to load DLL 'libmp3lame.32.dll': The specified module could not be found.
Run Code Online (Sandbox Code Playgroud)

我通过NuGet添加了库.我能够使用Windows窗体应用程序使该库工作正常,所以我认为问题是针对MVC4的.

我在这里尝试了图书馆作者的建议:https: //stackoverflow.com/a/20065606/910348

dll asp.net-mvc naudio

6
推荐指数
1
解决办法
4549
查看次数

为什么Owin启动顺序会影响Cookie身份验证

我将Owin配置为在身份验证时发出令牌和cookie:

public void Configuration(IAppBuilder app)
{
    var cookieOptions = new CookieAuthenticationOptions
    {
        AuthenticationMode = AuthenticationMode.Active,
        CookieHttpOnly = true, // JavaScript should use the Bearer
        //AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        CookieName = "MyCookie",
        LoginPath = new PathString("/app/index.html#/login"),
    };

    var oAuthServerOptions = new OAuthAuthorizationServerOptions
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
        Provider = new MyAuthorizationServerProvider(),
    };

    var oAuthBearerOptions = new OAuthBearerAuthenticationOptions
    {
    };

    // Must be registered in this order!
    app.UseCookieAuthentication(cookieOptions);
    app.UseOAuthAuthorizationServer(oAuthServerOptions);
    app.UseOAuthBearerAuthentication(oAuthBearerOptions);
 }
Run Code Online (Sandbox Code Playgroud)

效果很好-它同时为我的SPA发出了Bearer令牌以调用我的API和cookie,因此我的老式MVC页面也可以登录。

但是,如果我在声明要使用CookieAuth之前注册了OAuth服务器,则不会发出Cookie。换句话说,如果我这样做,那是行不通的:

app.UseOAuthAuthorizationServer(oAuthServerOptions);
app.UseCookieAuthentication(cookieOptions);
app.UseOAuthBearerAuthentication(oAuthBearerOptions);
Run Code Online (Sandbox Code Playgroud)

另外,如果我取消注释此行,它也不会发出Cookie: …

asp.net-mvc owin

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

如何用Unity3d的内置物理引擎绘制弹丸轨迹?

我正在研究一种能够对物理力量作出反应的抛射物的游戏.我在游戏中使用内置的Unity物理,但是我在射击之前绘制了射弹的轨迹.为此,我使用LineRenderer和一个简单的Physics类来计算位置:

public class SlingPhysics
{
    private Vector3 HalfGravity;
    private float ForceMultiplier;

    public void Init(Vector3 gravity, float slingForce, float mass, float drag)
    {
        HalfGravity = gravity / 2.0f;
        ForceMultiplier = slingForce / mass / (1 + drag);
    }

    public Vector3 GetPosition(Vector3 start, Vector3 direction, float timeDelta)
    {
        Vector3 pos = direction * (timeDelta * ForceMultiplier);
        pos.y += (HalfGravity.y * timeDelta * timeDelta);
        return start + pos;
    }

    /// <summary>
    /// Computes an array of Trajectory object positions by time.
    /// </summary> …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

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