开发者

Asp.Net mvc authentication, where do I put a custom session key?

I have a user database, to which I have access trough a web service. One of the web service method is something like this:

public void login(string name, string password, out user_key)

and in my controller I want to do something like this:

String key = repo.login(username, password); // a wraper on the login method
if(key ....)
  开发者_如何学Python  FormsAuthentication.SetAuthCookie(username, false); 

And my questions, here they come: This key is used for retrieving specific user data. Where do I put the key, so that I can have access to it? I mean is there a method for the FormsAuthentication class, because saying something like: Session["key"] = key doesn't look like a good practice to me. And what is the good practice here? so that bad-guys won't hack my session.


Don't quite understand what do you mean by

Session["key"] = key doesn't look like a good practice to me

I've been using something like Controller.HttpContext.Session for the longest period of time and don't feel slightest guilt at all.

If you want to worry about being hacked, then you should make sure that your GET parameter are properly sanitized before they are passed into database. That's important.


Sessions are separated from the authentication cookie in ASP.NET, so in order to take over a session the attacker would have to replicate both the authentication cookie and the session cookie.

You can write user information as part of the authentication ticket by using one of the constructors which accept userData before generating it and then reading it via the UserData property. Be aware though if this user key is sensitive then you may want to encrypt the authentication cookie. This is the default in ASP.NET but it's worth being specific and putting

<forms protection="All" >

into your web.config

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜