在呈现视图之前,是否可以让我的应用程序询问用户名和密码提示?就像在twitter API上获取有关您帐户的信息一样:
http://twitter.com/account/verify_credentials.xml
所以在渲染视图之前|| 文件它要求您插入用户名和密码,我认为这是直接在服务器上进行的,因为curl请求基于用户名:密码以及如下所示:
curl -u user:password http://twitter.com/account/verify_credentials.xml
Run Code Online (Sandbox Code Playgroud)
在我尝试使用相同的结构构建API时,我想知道如何在ASP.NET MVC C#上执行此操作.我已经在ruby rails上使用了它,它非常简单:
before_filter :authenticate
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == "foo" && password == "bar"
end
Run Code Online (Sandbox Code Playgroud)
我不认为[授权]过滤器是相同的,因为我认为它只是一个重定向,它将您重定向到基于帐户数据库的帐户内部控制器,在这种情况下,我将使用另一个数据库,特别是从webservice并在提交信息后进行验证.但我需要动作来要求用户并在其请求上传递凭据.
提前致谢
更新:
实际上,要请求一个需要此身份验证的页面(即Twitter),我必须在其请求中声明这一点
request.Credentials = new NetworkCredential("username", "password");
Run Code Online (Sandbox Code Playgroud)
这将反映出提示的用户名和密码.
所以,它是完全相同的,但从另一方面来说,如果可以根据请求向身份验证提示提供信息,我怎么能在请求中要求这种身份验证呢?
因此,每次有人试图通过示例向我的应用程序发出请求:
HTTP://为MyApplication /客户/ verify_credentials
它应该询问服务器提示符的用户名和密码,以便在curl上检索信息,例如它就像这样
curl -u user:password http://myapplication/clients/verify_credentials
Run Code Online (Sandbox Code Playgroud) 是否可以添加角色,但不能对值进行硬编码,如:
[Authorize(Roles="members, admin")]
Run Code Online (Sandbox Code Playgroud)
我想从数据库或配置文件中检索这些角色,如果我需要添加/删除控制器操作的角色,我将不需要重建应用程序.
我知道它可以用枚举来完成... http://www.vivienchevallier.com/Articles/create-a-custom-authorizeattribute-that-accepts-parameters-of-type-enum 但即使这仍然不灵活足以满足我的需求; 它仍然有点硬编码,即使它更干净.
我有一个在Azure上运行的ASP.NET MVC 5 Web应用程序作为webrole.
有没有办法轻松密码保护整个网站?我不想要任何注册或帐户处理,只需要一个密码就可以进入网站(也许是用户名,但这不是必需的).类似于.htaccess文件的东西.
我所看到的ASP.NET MVC身份验证的每个示例都附带了大量要实现的代码,而Azure似乎无法支持基本身份验证(至少不容易).
我使用以下代码进行授权(我在互联网上找到了它,并对其进行更改以供使用)
当我致电我的网址时,似乎授权有效

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class ClientAuthorizationAttribute : AuthorizationFilterAttribute
{
private bool _active = true;
public ClientAuthorizationAttribute()
{
}
public ClientAuthorizationAttribute(bool active)
{
_active = active;
}
public override void OnAuthorization(HttpActionContext actionContext)
{
if (_active)
{
var identity = ParseAuthorizationHeader(actionContext);
if (identity == null)
{
Challenge(actionContext);
return;
}
if (!OnAuthorizeUser(identity.Name, identity.Password, actionContext))
{
Challenge(actionContext);
return;
}
var principal = new GenericPrincipal(identity, null);
Thread.CurrentPrincipal = principal;
base.OnAuthorization(actionContext);
}
}
protected virtual bool OnAuthorizeUser(string clientId, string authId, HttpActionContext …Run Code Online (Sandbox Code Playgroud)