0

このようにフォーマットされているhttp URLからasp.net(mvc3)のユーザー名/パスワードを取得することは可能ですか?

http://user:password@example.com/path

それともftpプロトコルでのみ可能ですか?

4

1 に答える 1

2

例のユーザー名とパスワードは HTTP 基本認証を使用しています。これらは URL の一部ではなく、ヘッダー情報に含まれています。ASP.NET でこの情報にアクセスできます。次の記事を参照してください: Asp.Net WebAPI を使用した基本認証

public class BasicAuthenticationAttribute : System.Web.Http.Filters.ActionFilterAttribute {
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) {
        if (actionContext.Request.Headers.Authorization == null){
            // No Header Auth Info
            actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
        } else {
            // Get the auth token
            string authToken = actionContext.Request.Headers.Authorization.Parameter;
            // Decode the token from BASE64
            string decodedToken = Encoding.UTF8.GetString(Convert.FromBase64String(authToken));

            // Extract username and password from decoded token
            string username = decodedToken.Substring(0, decodedToken.IndexOf(":"));
            string password = decodedToken.Substring(decodedToken.IndexOf(":") + 1);
        }
    }
}
于 2012-10-20T16:10:21.480 に答える