开发者

Multiple Types of Authorizations in .NET MVC 2.0

I want to allow two types of authorization in my .NET MVC 2.0 app. One would be good old-fashion id/password (forms authentication) but I also want to be able to look at the header of the request page for an id/password as well. If开发者_运维技巧 that's provided, I want to authorize based upon that, by pass the form authentication and allow the user into the system. How's the best way to do that in MVC?


Don't think there is anything out of the box for this type of authentication. You could just read the request headers and check if they contain login information you need, and if they do, just do forms authentication but using the login info from the request header e.g.

string userId = String.Empty;
string password = String.Empty; 
foreach (var key in Request.Headers.AllKeys)
{
    if (key == "UserId")
        userId = Request.Headers[key];
    if (password == "Password")
        password = Request.Headers[password];
}

if (!String.IsNullOrEmpty(userId) && !String.IsNullOrEmpty(password))
    // attempt login
else
    // display login page


You can hook into authentication requests either in global.asax, or by writing a custom HTTPModule. The HTTPModule is particularly good at hooking into all the events in the ASP.NET pipeline, including authentication.

However, using this for authentication based on header content seems to be very high risk, knowing how easy it is to forge headers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜