我在编写PowerPivot要使用的安全WCF数据服务时遇到了一些麻烦。该服务运行正常,我可以毫不费力地使用PowerPivot中的数据。
我的问题是,当我在PowerPivot中输入数据Feed的用户ID和密码时(在数据Feed高级设置中),我似乎无法从WCF服务内部获取任何访问权限。我想使用用户ID和密码来对数据库进行身份验证,但我需要先了解它们。 :)
有没有关于如何专门为PowerPivot编写安全WCF数据服务的好例子?
非常感谢。
答案 0 :(得分:0)
我正在努力做同样的事情,经过一些研究发现这篇博文让我感动:
http://pfelix.wordpress.com/2011/04/21/wcf-web-api-self-hosting-https-and-http-basic-authentication/
简而言之,您需要做一些工作才能让Principal流向服务调用。
答案 1 :(得分:-1)
MSDN上有完整的可下载样本
使用PowerPivot客户端基本身份验证的WCF数据服务
https://code.msdn.microsoft.com/office/WCF-Data-Service-with-547e9341
更新
好的,现在我已经使用了链接中的代码(我在发布时正在研究)我知道它有用,所以这里是代码示例:
步骤1:编写HTTP处理程序以处理所有请求并执行身份验证(或发出401质询)。
namespace WebHostBasicAuth.Modules
{
public class BasicAuthHttpModule : IHttpModule
{
private const string Realm = "My Realm";
public void Init(HttpApplication context)
{
// Register event handlers
context.AuthenticateRequest += OnApplicationAuthenticateRequest;
context.EndRequest += OnApplicationEndRequest;
}
private static void SetPrincipal(IPrincipal principal)
{
Thread.CurrentPrincipal = principal;
if (HttpContext.Current != null)
{
HttpContext.Current.User = principal;
}
}
// TODO: Here is where you would validate the username and password.
private static bool CheckPassword(string username, string password)
{
return username == "user" && password == "password";
}
private static void AuthenticateUser(string credentials)
{
try
{
var encoding = Encoding.GetEncoding("iso-8859-1");
credentials = encoding.GetString(Convert.FromBase64String(credentials));
int separator = credentials.IndexOf(':');
string name = credentials.Substring(0, separator);
string password = credentials.Substring(separator + 1);
if (CheckPassword(name, password))
{
var identity = new GenericIdentity(name);
SetPrincipal(new GenericPrincipal(identity, null));
}
else
{
// Invalid username or password.
HttpContext.Current.Response.StatusCode = 401;
}
}
catch (FormatException)
{
// Credentials were not formatted correctly.
HttpContext.Current.Response.StatusCode = 401;
}
}
private static void OnApplicationAuthenticateRequest(object sender, EventArgs e)
{
var request = HttpContext.Current.Request;
var authHeader = request.Headers["Authorization"];
if (authHeader != null)
{
var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);
// RFC 2617 sec 1.2, "scheme" name is case-insensitive
if (authHeaderVal.Scheme.Equals("basic",
StringComparison.OrdinalIgnoreCase) &&
authHeaderVal.Parameter != null)
{
AuthenticateUser(authHeaderVal.Parameter);
}
}
}
// If the request was unauthorized, add the WWW-Authenticate header
// to the response.
private static void OnApplicationEndRequest(object sender, EventArgs e)
{
var response = HttpContext.Current.Response;
if (response.StatusCode == 401)
{
response.Headers.Add("WWW-Authenticate",
string.Format("Basic realm=\"{0}\"", Realm));
}
}
public void Dispose()
{
}
}
}
步骤2:通过web.config配置新的处理程序。
<system.webServer>
<modules>
<add name="BasicAuthHttpModule"
type="WebHostBasicAuth.Modules.BasicAuthHttpModule, YourAssemblyName"/>
</modules>
...
对Excel PowerPivot很重要
请参阅此错误:PowerPivot not sending Authorization header in Basic Authentication to OData Svc