1

I have a situation where I'm using Credentials auth successfully, but I sometimes need to be able to simply create an authenticated session from inside a service using nothing but the user's email address. How can I do that? I suppose I'm looking for something similar in intent to FormsAuthentication.SetAuthCookie(), but I haven't found it.

Here's my thinking so far. Assuming I have to construct this myself, I see this inside CredentialsAuthProvider.TryAuthenticate:

if (authRepo.TryAuthenticate(userName, password, out userAuth))
{
    session.PopulateWith(userAuth);
    session.IsAuthenticated = true;
    session.UserAuthId = userAuth.Id.ToString(CultureInfo.InvariantCulture);
    session.ProviderOAuthAccess = authRepo.GetUserOAuthProviders(session.UserAuthId)
        .ConvertAll(x => (IOAuthTokens)x);

    return true;
}

That seems to imply that I can do what's inside the block myself via a UserAuth object from IUserAuthRepository.GetUserAuthByUserName(). I don't think this is enough though, since the session isn't saved here. Working backwards, I found that TryAuthenticate is called by Authenticate which then goes on to call OnAuthenticated where other things happen including saving the session. Can I resolve an instance of CredentialsAuthProvider and call OnAuthenticated myself?

Am I on the right track, or is this the completely wrong way of going about this?

4

1 回答 1

1

sometimes need to be able to simply create an authenticated session from inside a service using nothing but the user's email address. How can I do that? I suppose I'm looking for something similar in intent to FormsAuthentication.SetAuthCookie()

I think the simplest, most FormsAuthentication.SetAuthCookie() way I can think of is to modify the AuthUserSession within in your service. The code below will get the Session, set IsAuthenticated to true, set the email and save the Session.

public class SomeService : Service
{
    public String Any(SomeRequest request)
    {
        //not sure you need it, but you could put some code here to verify the email is allowed to authenticate
        //and if true run the code below
        var sess = this.SessionAs<AuthUserSession>();
        sess.IsAuthenticated = true;
        sess.Email = "test@email.com";
        this.SaveSession(sess);

        return "success";
    }
}
于 2013-06-05T21:57:01.293 回答